public class AutowiredAnnotationBeanPostProcessor
extends org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
implements org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor, org.springframework.core.PriorityOrdered, org.springframework.beans.factory.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 standard 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
限定符和类型 | 字段和说明 |
---|---|
protected org.apache.commons.logging.Log |
logger |
构造器和说明 |
---|
AutowiredAnnotationBeanPostProcessor()
Create a new AutowiredAnnotationBeanPostProcessor
for Spring's standard
Autowired annotation. |
限定符和类型 | 方法和说明 |
---|---|
java.lang.reflect.Constructor<?>[] |
determineCandidateConstructors(java.lang.Class<?> beanClass,
java.lang.String beanName) |
protected boolean |
determineRequiredStatus(org.springframework.core.annotation.AnnotationAttributes ann)
Determine if the annotated field or method requires its dependency.
|
protected <T> java.util.Map<java.lang.String,T> |
findAutowireCandidates(java.lang.Class<T> type)
Obtain all beans of the given type as autowire candidates.
|
int |
getOrder() |
void |
postProcessMergedBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition beanDefinition,
java.lang.Class<?> beanType,
java.lang.String beanName) |
org.springframework.beans.PropertyValues |
postProcessPropertyValues(org.springframework.beans.PropertyValues pvs,
java.beans.PropertyDescriptor[] pds,
java.lang.Object bean,
java.lang.String beanName) |
void |
processInjection(java.lang.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 |
setAutowiredAnnotationType(java.lang.Class<? extends java.lang.annotation.Annotation> autowiredAnnotationType)
Set the 'autowired' annotation type, to be used on constructors, fields,
setter methods and arbitrary config methods.
|
void |
setAutowiredAnnotationTypes(java.util.Set<java.lang.Class<? extends java.lang.annotation.Annotation>> autowiredAnnotationTypes)
Set the 'autowired' annotation types, to be used on constructors, fields,
setter methods and arbitrary config methods.
|
void |
setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) |
void |
setOrder(int order) |
void |
setRequiredParameterName(java.lang.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
For example if using 'required=true' (the default), this value should be
true ; but if using 'optional=false', this value should be false . |
getEarlyBeanReference, postProcessAfterInitialization, postProcessAfterInstantiation, postProcessBeforeInitialization, postProcessBeforeInstantiation, predictBeanType
public AutowiredAnnotationBeanPostProcessor()
Autowired
annotation.
Also supports JSR-330's Inject
annotation, if available.
public void setAutowiredAnnotationType(java.lang.Class<? extends java.lang.annotation.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(java.util.Set<java.lang.Class<? extends java.lang.annotation.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(java.lang.String requiredParameterName)
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
.
public void setOrder(int order)
public int getOrder()
getOrder
在接口中 org.springframework.core.Ordered
public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
setBeanFactory
在接口中 org.springframework.beans.factory.BeanFactoryAware
public void postProcessMergedBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition beanDefinition, java.lang.Class<?> beanType, java.lang.String beanName)
postProcessMergedBeanDefinition
在接口中 org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor
public java.lang.reflect.Constructor<?>[] determineCandidateConstructors(java.lang.Class<?> beanClass, java.lang.String beanName) throws org.springframework.beans.factory.BeanCreationException
determineCandidateConstructors
在接口中 org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
determineCandidateConstructors
在类中 org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
org.springframework.beans.factory.BeanCreationException
public org.springframework.beans.PropertyValues postProcessPropertyValues(org.springframework.beans.PropertyValues pvs, java.beans.PropertyDescriptor[] pds, java.lang.Object bean, java.lang.String beanName) throws org.springframework.beans.factory.BeanCreationException
postProcessPropertyValues
在接口中 org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor
postProcessPropertyValues
在类中 org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
org.springframework.beans.factory.BeanCreationException
public void processInjection(java.lang.Object bean) throws org.springframework.beans.factory.BeanCreationException
@Autowired
.bean
- the target instance to processorg.springframework.beans.factory.BeanCreationException
- 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> java.util.Map<java.lang.String,T> findAutowireCandidates(java.lang.Class<T> type) throws org.springframework.beans.BeansException
type
- the type of the beanorg.springframework.beans.BeansException
- if bean retrieval failed