|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
public class AutowiredAnnotationBeanPostProcessor
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 carry 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 carry 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 default constructor (if present) will be used.
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.
setAutowiredAnnotationType(java.lang.Class extends java.lang.annotation.Annotation>)
,
Autowired
,
Value
Nested Class Summary | |
---|---|
private class |
AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement
Class representing injection information about an annotated field. |
private class |
AutowiredAnnotationBeanPostProcessor.AutowiredMethodElement
Class representing injection information about an annotated method. |
Field Summary | |
---|---|
private java.util.Set<java.lang.Class<? extends java.lang.annotation.Annotation>> |
autowiredAnnotationTypes
|
private ConfigurableListableBeanFactory |
beanFactory
|
private java.util.Map<java.lang.Class<?>,java.lang.reflect.Constructor<?>[]> |
candidateConstructorsCache
|
private java.util.Map<java.lang.Class<?>,InjectionMetadata> |
injectionMetadataCache
|
protected Log |
logger
|
private int |
order
|
private java.lang.String |
requiredParameterName
|
private boolean |
requiredParameterValue
|
Fields inherited from interface org.springframework.core.Ordered |
---|
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE |
Constructor Summary | |
---|---|
AutowiredAnnotationBeanPostProcessor()
Create a new AutowiredAnnotationBeanPostProcessor for Spring's standard Autowired annotation. |
Method Summary | ||
---|---|---|
private InjectionMetadata |
buildAutowiringMetadata(java.lang.Class<?> clazz)
|
|
java.lang.reflect.Constructor<?>[] |
determineCandidateConstructors(java.lang.Class<?> beanClass,
java.lang.String beanName)
Determine the candidate constructors to use for the given bean. |
|
protected boolean |
determineRequiredStatus(java.lang.annotation.Annotation annotation)
Determine if the annotated field or method requires its dependency. |
|
protected
|
findAutowireCandidates(java.lang.Class<T> type)
Obtain all beans of the given type as autowire candidates. |
|
private java.lang.annotation.Annotation |
findAutowiredAnnotation(java.lang.reflect.AccessibleObject ao)
|
|
private InjectionMetadata |
findAutowiringMetadata(java.lang.Class<?> clazz)
|
|
int |
getOrder()
Return the order value of this object, with a higher value meaning greater in terms of sorting. |
|
void |
postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition,
java.lang.Class<?> beanType,
java.lang.String beanName)
Post-process the given merged bean definition for the specified bean. |
|
PropertyValues |
postProcessPropertyValues(PropertyValues pvs,
java.beans.PropertyDescriptor[] pds,
java.lang.Object bean,
java.lang.String beanName)
Post-process the given property values before the factory applies them to the given bean. |
|
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 . |
|
private void |
registerDependentBeans(java.lang.String beanName,
java.util.Set<java.lang.String> autowiredBeanNames)
Register the specified bean as dependent on the autowired beans. |
|
private java.lang.Object |
resolvedCachedArgument(java.lang.String beanName,
java.lang.Object cachedArgument)
Resolve the specified cached method argument or field value. |
|
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(BeanFactory beanFactory)
Callback that supplies the owning factory to a bean instance. |
|
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 |
Methods inherited from class org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter |
---|
getEarlyBeanReference, postProcessAfterInitialization, postProcessAfterInstantiation, postProcessBeforeInitialization, postProcessBeforeInstantiation, predictBeanType |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface org.springframework.beans.factory.config.BeanPostProcessor |
---|
postProcessAfterInitialization, postProcessBeforeInitialization |
Field Detail |
---|
protected final Log logger
private final java.util.Set<java.lang.Class<? extends java.lang.annotation.Annotation>> autowiredAnnotationTypes
private java.lang.String requiredParameterName
private boolean requiredParameterValue
private int order
private ConfigurableListableBeanFactory beanFactory
private final java.util.Map<java.lang.Class<?>,java.lang.reflect.Constructor<?>[]> candidateConstructorsCache
private final java.util.Map<java.lang.Class<?>,InjectionMetadata> injectionMetadataCache
Constructor Detail |
---|
public AutowiredAnnotationBeanPostProcessor()
Autowired
annotation.
Also supports JSR-330's javax.inject.Inject
annotation, if available.
Method Detail |
---|
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)
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()
Ordered
Normally starting with 0, with Integer.MAX_VALUE
indicating the greatest value. Same order values will result
in arbitrary positions for the affected objects.
Higher values can be interpreted as lower priority. As a consequence, the object with the lowest value has highest priority (somewhat analogous to Servlet "load-on-startup" values).
getOrder
in interface Ordered
public void setBeanFactory(BeanFactory beanFactory) throws BeansException
BeanFactoryAware
Invoked after the population of normal bean properties
but before an initialization callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method.
setBeanFactory
in interface BeanFactoryAware
beanFactory
- owning BeanFactory (never null
).
The bean can immediately call methods on the factory.
BeansException
- in case of initialization errorsBeanInitializationException
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, java.lang.Class<?> beanType, java.lang.String beanName)
MergedBeanDefinitionPostProcessor
postProcessMergedBeanDefinition
in interface MergedBeanDefinitionPostProcessor
beanDefinition
- the merged bean definition for the beanbeanType
- the actual type of the managed bean instancebeanName
- the name of the beanpublic java.lang.reflect.Constructor<?>[] determineCandidateConstructors(java.lang.Class<?> beanClass, java.lang.String beanName) throws BeansException
SmartInstantiationAwareBeanPostProcessor
determineCandidateConstructors
in interface SmartInstantiationAwareBeanPostProcessor
determineCandidateConstructors
in class InstantiationAwareBeanPostProcessorAdapter
beanClass
- the raw class of the bean (never null
)beanName
- the name of the bean
null
if none specified
BeansException
- in case of errorspublic PropertyValues postProcessPropertyValues(PropertyValues pvs, java.beans.PropertyDescriptor[] pds, java.lang.Object bean, java.lang.String beanName) throws BeansException
InstantiationAwareBeanPostProcessor
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.
postProcessPropertyValues
in interface InstantiationAwareBeanPostProcessor
postProcessPropertyValues
in class InstantiationAwareBeanPostProcessorAdapter
pvs
- 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 bean
null
to skip property population
BeansException
- in case of errorsMutablePropertyValues
public void processInjection(java.lang.Object bean) throws BeansException
@Autowired
.
bean
- the target instance to process
BeansException
- if autowiring failedprivate InjectionMetadata findAutowiringMetadata(java.lang.Class<?> clazz)
private InjectionMetadata buildAutowiringMetadata(java.lang.Class<?> clazz)
private java.lang.annotation.Annotation findAutowiredAnnotation(java.lang.reflect.AccessibleObject ao)
protected <T> java.util.Map<java.lang.String,T> findAutowireCandidates(java.lang.Class<T> type) throws BeansException
type
- the type of the bean
BeansException
- if bean retrieval failedprotected boolean determineRequiredStatus(java.lang.annotation.Annotation annotation)
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.
annotation
- the Autowired annotation
private void registerDependentBeans(java.lang.String beanName, java.util.Set<java.lang.String> autowiredBeanNames)
private java.lang.Object resolvedCachedArgument(java.lang.String beanName, java.lang.Object cachedArgument)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |