类 AutowiredAnnotationBeanPostProcessor
- java.lang.Object
- org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
- org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
- 所有已实现的接口:
Aware
,BeanFactoryAware
,BeanPostProcessor
,InstantiationAwareBeanPostProcessor
,SmartInstantiationAwareBeanPostProcessor
,MergedBeanDefinitionPostProcessor
,Ordered
,PriorityOrdered
public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware
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 JSR-330's
@Inject
annotation, if available, as a direct alternative to Spring's own@Autowired
.Autowired Constructors
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 totrue
, 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.Autowired Fields
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.
Autowired Methods
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.
Annotation Config vs. XML Config
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 customAutowiredAnnotationBeanPostProcessor
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.
@Lookup Methods
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 ofgetBean(Class, args)
andgetBean(String, args)
. See@Lookup's javadoc
for details.- 从以下版本开始:
- 2.5
- 作者:
- Juergen Hoeller, Mark Fisher, Stephane Nicoll, Sebastien Deleuze, Sam Brannen
- 另请参阅:
setAutowiredAnnotationType(java.lang.Class<? extends java.lang.annotation.Annotation>)
,Autowired
,Value
字段概要
从接口继承的字段 org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
构造器概要
构造器 构造器 说明 AutowiredAnnotationBeanPostProcessor()
Create a newAutowiredAnnotationBeanPostProcessor
for Spring's standard@Autowired
and@Value
annotations.
方法概要
所有方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 Constructor<?>[]
determineCandidateConstructors(Class<?> beanClass, String beanName)
Determine the candidate constructors to use for the given bean.protected boolean
determineRequiredStatus(AnnotationAttributes ann)
已过时。since 5.2, in favor ofdetermineRequiredStatus(MergedAnnotation)
protected boolean
determineRequiredStatus(MergedAnnotation<?> 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()
Get the order value of this object.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)
已过时。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 one of the configured 'autowired' annotation types.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 an attribute of the annotation that specifies whether it is required.void
setRequiredParameterValue(boolean requiredParameterValue)
Set the boolean value that marks a dependency as required.从类继承的方法 org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
getEarlyBeanReference, postProcessAfterInitialization, postProcessAfterInstantiation, postProcessBeforeInitialization, postProcessBeforeInstantiation, predictBeanType
从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
从接口继承的方法 org.springframework.beans.factory.config.BeanPostProcessor
postProcessAfterInitialization, postProcessBeforeInitialization
构造器详细资料
AutowiredAnnotationBeanPostProcessor
public AutowiredAnnotationBeanPostProcessor()
Create a newAutowiredAnnotationBeanPostProcessor
for Spring's standard@Autowired
and@Value
annotations.Also supports JSR-330's
@Inject
annotation, if available.
方法详细资料
setAutowiredAnnotationType
public void setAutowiredAnnotationType(Class<? extends Annotation> autowiredAnnotationType)
Set the 'autowired' annotation type, to be used on constructors, fields, setter methods, and arbitrary config methods.The default autowired annotation types are the Spring-provided
@Autowired
and@Value
annotations as well as JSR-330's@Inject
annotation, if available.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.
setAutowiredAnnotationTypes
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.The default autowired annotation types are the Spring-provided
@Autowired
and@Value
annotations as well as JSR-330's@Inject
annotation, if available.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.
setRequiredParameterName
public void setRequiredParameterName(String requiredParameterName)
Set the name of an attribute of the annotation that specifies whether it is required.
setRequiredParameterValue
public 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 befalse
.
setOrder
public void setOrder(int order)
getOrder
public int getOrder()
从接口复制的说明:Ordered
Get the order value of this object.Higher values are interpreted as lower priority. As a consequence, the object with the lowest value has the highest priority (somewhat analogous to Servlet
load-on-startup
values).Same order values will result in arbitrary sort positions for the affected objects.
- 指定者:
getOrder
在接口中Ordered
- 返回:
- the order value
- 另请参阅:
Ordered.HIGHEST_PRECEDENCE
,Ordered.LOWEST_PRECEDENCE
setBeanFactory
public void setBeanFactory(BeanFactory beanFactory)
从接口复制的说明:BeanFactoryAware
Callback that supplies the owning factory to a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method.- 指定者:
setBeanFactory
在接口中BeanFactoryAware
- 参数:
beanFactory
- owning BeanFactory (nevernull
). The bean can immediately call methods on the factory.- 另请参阅:
BeanInitializationException
postProcessMergedBeanDefinition
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName)
从接口复制的说明:MergedBeanDefinitionPostProcessor
Post-process the given merged bean definition for the specified bean.- 指定者:
postProcessMergedBeanDefinition
在接口中MergedBeanDefinitionPostProcessor
- 参数:
beanDefinition
- the merged bean definition for the beanbeanType
- the actual type of the managed bean instancebeanName
- the name of the bean- 另请参阅:
AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Class<?>, java.lang.String)
resetBeanDefinition
public void resetBeanDefinition(String beanName)
从接口复制的说明:MergedBeanDefinitionPostProcessor
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.The default implementation is empty.
- 指定者:
resetBeanDefinition
在接口中MergedBeanDefinitionPostProcessor
- 参数:
beanName
- the name of the bean- 另请参阅:
DefaultListableBeanFactory.resetBeanDefinition(java.lang.String)
determineCandidateConstructors
@Nullable public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeanCreationException
从接口复制的说明:SmartInstantiationAwareBeanPostProcessor
Determine the candidate constructors to use for the given bean.The default implementation returns
null
.- 指定者:
determineCandidateConstructors
在接口中SmartInstantiationAwareBeanPostProcessor
- 覆盖:
determineCandidateConstructors
在类中InstantiationAwareBeanPostProcessorAdapter
- 参数:
beanClass
- the raw class of the bean (nevernull
)beanName
- the name of the bean- 返回:
- the candidate constructors, or
null
if none specified - 抛出:
BeanCreationException
postProcessProperties
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
从接口复制的说明:InstantiationAwareBeanPostProcessor
Post-process the given property values before the factory applies them to the given bean, without any need for property descriptors.Implementations should return
null
(the default) if they provide a customInstantiationAwareBeanPostProcessor.postProcessPropertyValues(org.springframework.beans.PropertyValues, java.beans.PropertyDescriptor[], java.lang.Object, java.lang.String)
implementation, andpvs
otherwise. In a future version of this interface (withInstantiationAwareBeanPostProcessor.postProcessPropertyValues(org.springframework.beans.PropertyValues, java.beans.PropertyDescriptor[], java.lang.Object, java.lang.String)
removed), the default implementation will return the givenpvs
as-is directly.- 指定者:
postProcessProperties
在接口中InstantiationAwareBeanPostProcessor
- 覆盖:
postProcessProperties
在类中InstantiationAwareBeanPostProcessorAdapter
- 参数:
pvs
- the property values that the factory is about to apply (nevernull
)bean
- the bean instance created, but whose properties have not yet been setbeanName
- the name of the bean- 返回:
- the actual property values to apply to the given bean (can be the passed-in PropertyValues instance), or
null
which proceeds with the existing properties but specifically continues with a call toInstantiationAwareBeanPostProcessor.postProcessPropertyValues(org.springframework.beans.PropertyValues, java.beans.PropertyDescriptor[], java.lang.Object, java.lang.String)
(requiring initializedPropertyDescriptor
s for the current bean class) - 另请参阅:
InstantiationAwareBeanPostProcessor.postProcessPropertyValues(org.springframework.beans.PropertyValues, java.beans.PropertyDescriptor[], java.lang.Object, java.lang.String)
postProcessPropertyValues
@Deprecated public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
已过时。从接口复制的说明:InstantiationAwareBeanPostProcessor
Post-process the given property values before the factory applies them to the given bean. Allows for checking whether all dependencies have been satisfied, for example based on a "Required" annotation on bean property setters.Also 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
在接口中InstantiationAwareBeanPostProcessor
- 覆盖:
postProcessPropertyValues
在类中InstantiationAwareBeanPostProcessorAdapter
- 参数:
pvs
- the property values that the factory is about to apply (nevernull
)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 bean- 返回:
- the actual property values to apply to the given bean (can be the passed-in PropertyValues instance), or
null
to skip property population - 另请参阅:
InstantiationAwareBeanPostProcessor.postProcessProperties(org.springframework.beans.PropertyValues, java.lang.Object, java.lang.String)
,MutablePropertyValues
processInjection
public void processInjection(Object bean) throws BeanCreationException
'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.- 参数:
bean
- the target instance to process- 抛出:
BeanCreationException
- if autowiring failed- 另请参阅:
setAutowiredAnnotationTypes(Set)
determineRequiredStatus
protected boolean determineRequiredStatus(MergedAnnotation<?> ann)
Determine if the annotated field or method requires its dependency.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 annotation- 返回:
- whether the annotation indicates that a dependency is required
determineRequiredStatus
@Deprecated protected boolean determineRequiredStatus(AnnotationAttributes ann)
已过时。since 5.2, in favor ofdetermineRequiredStatus(MergedAnnotation)
Determine if the annotated field or method requires its dependency.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 annotation- 返回:
- whether the annotation indicates that a dependency is required
findAutowireCandidates
protected <T> Map<String,T> findAutowireCandidates(Class<T> type) throws BeansException
Obtain all beans of the given type as autowire candidates.- 参数:
type
- the type of the bean- 返回:
- the target beans, or an empty Collection if no bean of this type is found
- 抛出:
BeansException
- if bean retrieval failed