public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements MergedBeanDefinitionPostProcessor, org.springframework.core.PriorityOrdered, BeanFactoryAware
BeanPostProcessor implementation
that autowires annotated fields, setter methods and arbitrary config methods.
Such members to be injected are detected through a Java 5 annotation: by default,
Spring's @Autowired and @Value annotations.
Also supports JSR-330's @Inject annotation,
if available, as a direct alternative to Spring's own @Autowired.
Only one constructor (at max) of any given bean class may declare this annotation
with the 'required' parameter set to true, indicating the constructor
to autowire when used as a Spring bean. 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.
Note: 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(java.lang.Class<? extends java.lang.annotation.Annotation>),
Autowired,
Value| Constructor and Description |
|---|
AutowiredAnnotationBeanPostProcessor()
Create a new
AutowiredAnnotationBeanPostProcessor for Spring's
standard @Autowired annotation. |
| Modifier and Type | Method and Description |
|---|---|
Constructor<?>[] |
determineCandidateConstructors(Class<?> beanClass,
String beanName)
Determine the candidate constructors to use for the given bean.
|
protected boolean |
determineRequiredStatus(org.springframework.core.annotation.AnnotationAttributes ann)
Determine if the annotated field or method requires its dependency.
|
protected <T> Map<String,T> |
findAutowireCandidates(Class<T> type)
Obtain all beans of the given type as autowire candidates.
|
int |
getOrder() |
void |
postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition,
Class<?> beanType,
String beanName)
Post-process the given merged bean definition for the specified bean.
|
PropertyValues |
postProcessProperties(PropertyValues pvs,
Object bean,
String beanName)
Post-process the given property values before the factory applies them
to the given bean, without any need for property descriptors.
|
PropertyValues |
postProcessPropertyValues(PropertyValues pvs,
PropertyDescriptor[] pds,
Object bean,
String beanName)
Deprecated.
|
void |
processInjection(Object bean)
'Native' processing method for direct calls with an arbitrary target instance,
resolving all of its fields and methods which are annotated with
@Autowired. |
void |
resetBeanDefinition(String beanName)
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.
|
void |
setAutowiredAnnotationType(Class<? extends Annotation> autowiredAnnotationType)
Set the 'autowired' annotation type, to be used on constructors, fields,
setter methods and arbitrary config methods.
|
void |
setAutowiredAnnotationTypes(Set<Class<? extends Annotation>> autowiredAnnotationTypes)
Set the 'autowired' annotation types, to be used on constructors, fields,
setter methods and arbitrary config methods.
|
void |
setBeanFactory(BeanFactory beanFactory)
Callback that supplies the owning factory to a bean instance.
|
void |
setOrder(int order) |
void |
setRequiredParameterName(String requiredParameterName)
Set the name of a parameter of the annotation that specifies whether it is required.
|
void |
setRequiredParameterValue(boolean requiredParameterValue)
Set the boolean value that marks a dependency as required
|
getEarlyBeanReference, postProcessAfterInitialization, postProcessAfterInstantiation, postProcessBeforeInitialization, postProcessBeforeInstantiation, predictBeanTypeclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitpostProcessAfterInitialization, postProcessBeforeInitializationprotected final Log logger
public AutowiredAnnotationBeanPostProcessor()
AutowiredAnnotationBeanPostProcessor for Spring's
standard @Autowired annotation.
Also supports JSR-330's @Inject annotation,
if available.
public void setAutowiredAnnotationType(Class<? extends Annotation> autowiredAnnotationType)
The default autowired annotation type is the Spring-provided Autowired
annotation, as well as Value.
This setter property exists so that developers can provide their own (non-Spring-specific) annotation type to indicate that a member is supposed to be autowired.
public void setAutowiredAnnotationTypes(Set<Class<? extends Annotation>> autowiredAnnotationTypes)
The default autowired annotation type is the Spring-provided Autowired
annotation, as well as Value.
This setter property exists so that developers can provide their own (non-Spring-specific) annotation types to indicate that a member is supposed to be autowired.
public void setRequiredParameterName(String requiredParameterName)
setRequiredParameterValue(boolean)public void setRequiredParameterValue(boolean requiredParameterValue)
For example if using 'required=true' (the default), this value should be
true; but if using 'optional=false', this value should be false.
setRequiredParameterName(String)public void setOrder(int order)
public int getOrder()
getOrder in interface org.springframework.core.Orderedpublic void setBeanFactory(BeanFactory beanFactory)
BeanFactoryAwareInvoked after the population of normal bean properties
but before an initialization callback such as
InitializingBean.afterPropertiesSet() or a custom init-method.
setBeanFactory in interface BeanFactoryAwarebeanFactory - owning BeanFactory (never null).
The bean can immediately call methods on the factory.BeanInitializationExceptionpublic void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName)
MergedBeanDefinitionPostProcessorpostProcessMergedBeanDefinition in interface MergedBeanDefinitionPostProcessorbeanDefinition - the merged bean definition for the beanbeanType - the actual type of the managed bean instancebeanName - the name of the beanAbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Class<?>, java.lang.String)public void resetBeanDefinition(String beanName)
MergedBeanDefinitionPostProcessorThe default implementation is empty.
resetBeanDefinition in interface MergedBeanDefinitionPostProcessorbeanName - the name of the beanDefaultListableBeanFactory.resetBeanDefinition(java.lang.String)@Nullable public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeanCreationException
SmartInstantiationAwareBeanPostProcessorThe default implementation returns null.
determineCandidateConstructors in interface SmartInstantiationAwareBeanPostProcessordetermineCandidateConstructors in class InstantiationAwareBeanPostProcessorAdapterbeanClass - the raw class of the bean (never null)beanName - the name of the beannull if none specifiedBeanCreationExceptionpublic PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
InstantiationAwareBeanPostProcessorImplementations should return null (the default) if they provide a custom
InstantiationAwareBeanPostProcessor.postProcessPropertyValues(org.springframework.beans.PropertyValues, java.beans.PropertyDescriptor[], java.lang.Object, java.lang.String) implementation, and pvs otherwise.
In a future version of this interface (with InstantiationAwareBeanPostProcessor.postProcessPropertyValues(org.springframework.beans.PropertyValues, java.beans.PropertyDescriptor[], java.lang.Object, java.lang.String) removed),
the default implementation will return the given pvs as-is directly.
postProcessProperties in interface InstantiationAwareBeanPostProcessorpostProcessProperties in class InstantiationAwareBeanPostProcessorAdapterpvs - the property values that the factory is about to apply (never null)bean - the bean instance created, but whose properties have not yet been setbeanName - the name of the beannull which proceeds with the existing properties
but specifically continues with a call to InstantiationAwareBeanPostProcessor.postProcessPropertyValues(org.springframework.beans.PropertyValues, java.beans.PropertyDescriptor[], java.lang.Object, java.lang.String)
(requiring initialized PropertyDescriptors for the current bean class)InstantiationAwareBeanPostProcessor.postProcessPropertyValues(org.springframework.beans.PropertyValues, java.beans.PropertyDescriptor[], java.lang.Object, java.lang.String)@Deprecated public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
InstantiationAwareBeanPostProcessorAlso allows for replacing the property values to apply, typically through creating a new MutablePropertyValues instance based on the original PropertyValues, adding or removing specific values.
The default implementation returns the given pvs as-is.
postProcessPropertyValues in interface InstantiationAwareBeanPostProcessorpostProcessPropertyValues in class InstantiationAwareBeanPostProcessorAdapterpvs - the property values that the factory is about to apply (never null)pds - the relevant property descriptors for the target bean (with ignored
dependency types - which the factory handles specifically - already filtered out)bean - the bean instance created, but whose properties have not yet been setbeanName - the name of the beannull to skip property populationInstantiationAwareBeanPostProcessor.postProcessProperties(org.springframework.beans.PropertyValues, java.lang.Object, java.lang.String),
MutablePropertyValuespublic void processInjection(Object bean) throws BeanCreationException
@Autowired.bean - the target instance to processBeanCreationException - if autowiring failedprotected boolean determineRequiredStatus(org.springframework.core.annotation.AnnotationAttributes ann)
A 'required' dependency means that autowiring should fail when no beans are found. Otherwise, the autowiring process will simply bypass the field or method when no beans are found.
ann - the Autowired annotationprotected <T> Map<String,T> findAutowireCandidates(Class<T> type) throws BeansException
type - the type of the beanBeansException - if bean retrieval failed