BeanPostProcessor
implementation that autowires annotated fields, setter methods, and arbitrary
config methods. Such members to be injected are detected through annotations:
by default, Spring's @Autowired and @Value
annotations.
Also supports the common @Inject annotation,
if available, as a direct alternative to Spring's own @Autowired.
Additionally, it retains support for the javax.inject.Inject variant
dating back to the original JSR-330 specification (as known from Java EE 6-8).
Only one constructor of any given bean class may declare this annotation with
the 'required' attribute set to true, indicating the constructor
to autowire when used as a Spring bean. Furthermore, if the 'required' attribute
is set to true, only a single constructor may be annotated with
@Autowired. If multiple non-required constructors declare the
annotation, they will be considered as candidates for autowiring. The constructor
with the greatest number of dependencies that can be satisfied by matching beans
in the Spring container will be chosen. If none of the candidates can be satisfied,
then a primary/default constructor (if present) will be used. If a class only
declares a single constructor to begin with, it will always be used, even if not
annotated. An annotated constructor does not have to be public.
Fields are injected right after construction of a bean, before any config methods are invoked. Such a config field does not have to be public.
Config methods may have an arbitrary name and any number of arguments; each of those arguments will be autowired with a matching bean in the Spring container. Bean property setter methods are effectively just a special case of such a general config method. Config methods do not have to be public.
A default AutowiredAnnotationBeanPostProcessor will be registered
by the "context:annotation-config" and "context:component-scan" XML tags.
Remove or turn off the default annotation configuration there if you intend
to specify a custom AutowiredAnnotationBeanPostProcessor bean definition.
Note
Annotation injection will be performed before XML injection; thus the latter configuration will override the former for properties wired through both approaches.
In addition to regular injection points as discussed above, this post-processor
also handles Spring's @Lookup annotation which identifies lookup
methods to be replaced by the container at runtime. This is essentially a type-safe
version of getBean(Class, args) and getBean(String, args).
See @Lookup's javadoc for details.
setAutowiredAnnotationType, Autowired, Value
| Modifier and Type | Class and Description |
|---|---|
| private static class | AutowiredAnnotationBeanPostProcessor.
|
| private abstract static class | AutowiredAnnotationBeanPostProcessor.
Base class representing injection information. |
| private class | AutowiredAnnotationBeanPostProcessor.
Class representing injection information about an annotated field. |
| private class | AutowiredAnnotationBeanPostProcessor.
Class representing injection information about an annotated method. |
| private static class | AutowiredAnnotationBeanPostProcessor.
DependencyDescriptor variant with a pre-resolved target bean name. |
| Modifier and Type | Field and Description |
|---|---|
| private final Set | |
| private ConfigurableListableBeanFactory | |
| private final Map | |
| private static final Constructor | |
| private final Map | |
| protected final Log | |
| private final Set | |
| private MetadataReaderFactory | |
| private int | |
| private String | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public | AutowiredAnnotationBeanPostProcessor()
Create a new |
| Modifier and Type | Method and Description |
|---|---|
| private InjectionMetadata | |
| private void | |
| public Class | determineBeanType(Class<?>
the raw class of the bean beanClass, String the name of the bean beanName)Overrides default org. Determine the type of the bean to be eventually returned from this
processor's |
| public Constructor | determineCandidateConstructors(Class<?>
the raw class of the bean (never beanClass, final String null)the name of the bean beanName)Overrides default org. Determine the candidate constructors to use for the given bean. |
| protected boolean | Returns: whether the annotation indicates that a dependency is requiredthe Autowired annotation ann)Determine if the annotated field or method requires its dependency. |
| private MergedAnnotation | |
| private InjectionMetadata | |
| private InjectionMetadata | |
| private AutowireCandidateResolver | |
| private Collection | |
| public int | |
| public void | postProcessMergedBeanDefinition(RootBeanDefinition
the merged bean definition for the bean beanDefinition, Class<?> the actual type of the managed bean instance beanType, String the name of the bean beanName)Implements org. Post-process the given merged bean definition for the specified bean. |
| public PropertyValues | postProcessProperties(PropertyValues
the property values that the factory is about to apply (never pvs, Object null)the bean instance created, but whose properties have not yet been set bean, String the name of the bean beanName)Overrides default org. Post-process the given property values before the factory applies them to the given bean. |
| public BeanRegistrationAotContribution | processAheadOfTime(RegisteredBean
the registered bean to process registeredBean)Implements org. Process the given |
| public void | processInjection(Object
the target instance to process bean)Native processing method for direct calls with an arbitrary target instance, resolving all of its fields and methods which are annotated with one of the configured 'autowired' annotation types. |
| private void | registerDependentBeans(String beanName, Set<String> autowiredBeanNames)
Register the specified bean as dependent on the autowired beans. |
| public void | resetBeanDefinition(String
the name of the bean beanName)Overrides default org. A notification that the bean definition for the specified name has been reset, and that this post-processor should clear any metadata for the affected bean. |
| private Object | resolveCachedArgument(String beanName, Object cachedArgument)
Resolve the specified cached method argument or field value. |
| public void | setAutowiredAnnotationType(Class<? extends Annotation> autowiredAnnotationType)
Set the 'autowired' annotation type, to be used on constructors, fields, setter methods, and arbitrary config methods. |
| public void | setAutowiredAnnotationTypes(Set<Class<? extends Annotation>> autowiredAnnotationTypes)
Set the 'autowired' annotation types, to be used on constructors, fields, setter methods, and arbitrary config methods. |
| public void | setBeanFactory(BeanFactory
owning BeanFactory (never beanFactory)null).
The bean can immediately call methods on the factory.Implements org. Callback that supplies the owning factory to a bean instance. |
| public void | |
| public void | setRequiredParameterName(String requiredParameterName)
Set the name of an attribute of the annotation that specifies whether it is required. |
| public void | setRequiredParameterValue(boolean requiredParameterValue)
Set the boolean value that marks a dependency as required. |
| private List | sortMethodElements(List<InjectionMetadata.
Sort the method elements via ASM for deterministic declaration order if possible. |