org.springframework.beans.factory.annotation
Class AutowiredAnnotationBeanPostProcessor

java.lang.Object
  extended by org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
      extended by org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
All Implemented Interfaces:
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 a Java 5 annotation: by default, Spring's Autowired annotation.

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. Such 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.

Since:
2.5
Author:
Juergen Hoeller, Mark Fisher
See Also:
setAutowiredAnnotationType(java.lang.Class), Autowired, CommonAnnotationBeanPostProcessor

Field Summary
protected  org.apache.commons.logging.Log logger
           
 
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
 
Constructor Summary
AutowiredAnnotationBeanPostProcessor()
           
 
Method Summary
 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
<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()
          Return the order value of this object, with a higher value meaning greater in terms of sorting.
 boolean postProcessAfterInstantiation(java.lang.Object bean, java.lang.String beanName)
          Perform operations after the bean has been instantiated, via a constructor or factory method, but before Spring property population (from explicit properties or autowiring) occurs.
 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.
 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.lang.Class<? extends java.lang.annotation.Annotation>[] autowiredAnnotationTypes)
          Set the 'autowired' annotation type, 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, 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

logger

protected final org.apache.commons.logging.Log logger
Constructor Detail

AutowiredAnnotationBeanPostProcessor

public AutowiredAnnotationBeanPostProcessor()
Method Detail

setAutowiredAnnotationTypes

public void setAutowiredAnnotationTypes(java.lang.Class<? extends java.lang.annotation.Annotation>[] autowiredAnnotationTypes)
Set the 'autowired' annotation type, to be used on constructors, fields, setter methods and arbitrary config methods.

The default autowired annotation type is the Spring-provided Autowired annotation.

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.


setAutowiredAnnotationType

public 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.

The default autowired annotation type is the Spring-provided Autowired annotation.

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.


setRequiredParameterName

public void setRequiredParameterName(java.lang.String requiredParameterName)
Set the name of a parameter of the annotation that specifies whether it is required.

See Also:
setRequiredParameterValue(boolean)

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 be false.

See Also:
setRequiredParameterName(String)

setOrder

public void setOrder(int order)

getOrder

public int getOrder()
Description copied from interface: Ordered
Return the order value of this object, with a higher value meaning greater in terms of sorting.

Normally starting with 0 or 1, with Ordered.LOWEST_PRECEDENCE indicating greatest. Same order values will result in arbitrary positions for the affected objects.

Higher value can be interpreted as lower priority, consequently the first object has highest priority (somewhat analogous to Servlet "load-on-startup" values).

Note that order values below 0 are reserved for framework purposes. Application-specified values should always be 0 or greater, with only framework components (internal or third-party) supposed to use lower values.

Specified by:
getOrder in interface Ordered
Returns:
the order value
See Also:
Ordered.LOWEST_PRECEDENCE

setBeanFactory

public void setBeanFactory(BeanFactory beanFactory)
                    throws BeansException
Description copied from interface: 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.

Specified by:
setBeanFactory in interface BeanFactoryAware
Parameters:
beanFactory - owning BeanFactory (never null). The bean can immediately call methods on the factory.
Throws:
BeansException - in case of initialization errors
See Also:
BeanInitializationException

postProcessMergedBeanDefinition

public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition,
                                            java.lang.Class beanType,
                                            java.lang.String beanName)
Description copied from interface: MergedBeanDefinitionPostProcessor
Post-process the given merged bean definition for the specified bean.

Specified by:
postProcessMergedBeanDefinition in interface MergedBeanDefinitionPostProcessor
Parameters:
beanDefinition - the merged bean definition for the bean
beanType - the actual type of the managed bean instance
beanName - the name of the bean

determineCandidateConstructors

public java.lang.reflect.Constructor[] determineCandidateConstructors(java.lang.Class beanClass,
                                                                      java.lang.String beanName)
                                                               throws BeansException
Description copied from interface: SmartInstantiationAwareBeanPostProcessor
Determine the candidate constructors to use for the given bean.

Specified by:
determineCandidateConstructors in interface SmartInstantiationAwareBeanPostProcessor
Overrides:
determineCandidateConstructors in class InstantiationAwareBeanPostProcessorAdapter
Parameters:
beanClass - the raw class of the bean (never null)
beanName - the name of the bean
Returns:
the candidate constructors, or null if none specified
Throws:
BeansException - in case of errors

postProcessAfterInstantiation

public boolean postProcessAfterInstantiation(java.lang.Object bean,
                                             java.lang.String beanName)
                                      throws BeansException
Description copied from interface: InstantiationAwareBeanPostProcessor
Perform operations after the bean has been instantiated, via a constructor or factory method, but before Spring property population (from explicit properties or autowiring) occurs.

This is the ideal callback for performing field injection on the given bean instance. See Spring's own AutowiredAnnotationBeanPostProcessor for a typical example.

Specified by:
postProcessAfterInstantiation in interface InstantiationAwareBeanPostProcessor
Overrides:
postProcessAfterInstantiation in class InstantiationAwareBeanPostProcessorAdapter
Parameters:
bean - the bean instance created, with properties not having been set yet
beanName - the name of the bean
Returns:
true if properties should be set on the bean; false if property population should be skipped. Normal implementations should return true. Returning false will also prevent any subsequent InstantiationAwareBeanPostProcessor instances being invoked on this bean instance.
Throws:
BeansException - in case of errors

postProcessPropertyValues

public PropertyValues postProcessPropertyValues(PropertyValues pvs,
                                                java.beans.PropertyDescriptor[] pds,
                                                java.lang.Object bean,
                                                java.lang.String beanName)
                                         throws BeansException
Description copied from interface: 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.

Specified by:
postProcessPropertyValues in interface InstantiationAwareBeanPostProcessor
Overrides:
postProcessPropertyValues in class InstantiationAwareBeanPostProcessorAdapter
Parameters:
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 set
beanName - the name of the bean
Returns:
the actual property values to apply to to the given bean (can be the passed-in PropertyValues instance), or null to skip property population
Throws:
BeansException - in case of errors
See Also:
MutablePropertyValues

processInjection

public void processInjection(java.lang.Object bean)
                      throws BeansException
'Native' processing method for direct calls with an arbitrary target instance, resolving all of its fields and methods which are annotated with @Autowired.

Parameters:
bean - the target instance to process
Throws:
BeansException

findAutowireCandidates

protected <T> java.util.Map<java.lang.String,T> findAutowireCandidates(java.lang.Class<T> type)
                                                            throws BeansException
Obtain all beans of the given type as autowire candidates.

Parameters:
type - the type of the bean
Returns:
the target beans, or an empty Collection if no bean of this type is found
Throws:
BeansException - if bean retrieval failed

determineRequiredStatus

protected boolean determineRequiredStatus(java.lang.annotation.Annotation annotation)
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.

Parameters:
annotation - the Autowired annotation
Returns:
whether the annotation indicates that a dependency is required