Package org.springframework.beans.factory.annotation

# Class AutowiredAnnotationBeanPostProcessor

java.lang.Object
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor

All Implemented Interfaces:
  `BeanRegistrationAotProcessor`, `Aware`, `BeanFactoryAware`, `BeanPostProcessor`, `InstantiationAwareBeanPostProcessor`, `SmartInstantiationAwareBeanPostProcessor`, `MergedBeanDefinitionPostProcessor`, `org.springframework.core.Ordered`, `org.springframework.core.PriorityOrdered`

public class AutowiredAnnotationBeanPostProcessor extends Object implements SmartInstantiationAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor, BeanRegistrationAotProcessor, 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 annotations: by default, Spring's `@Autowired` and `@Value` annotations.

Also supports the common `@Inject` annotation, if available, as a direct alternative to Spring's own `@Autowired`. Additionally, it retains support for the `javax.inject.Inject` variant dating back to the original JSR-330 specification (as known from Java EE 6-8).

### 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 to `true`, 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 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.

### @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 of `getBean(Class, args)` and `getBean(String, args)`. See `@Lookup's javadoc` for details.

Since:
  2.5
Author:
  Juergen Hoeller, Mark Fisher, Stephane Nicoll, Sebastien Deleuze, Sam Brannen, Phillip Webb
See Also:
  - `setAutowiredAnnotationType(java.lang.Class<? extends java.lang.annotation.Annotation>)`
  - `Autowired`
  - `Value`

## Field Summary

Fields
Modifier and Type
Field
Description
`protected final org.apache.commons.logging.Log`
`logger`

### Fields inherited from interface org.springframework.beans.factory.aot.BeanRegistrationAotProcessor

`IGNORE_REGISTRATION_ATTRIBUTE`

### Fields inherited from interface org.springframework.core.Ordered

`HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE`

## Constructor Summary

Constructors
Constructor
Description
`AutowiredAnnotationBeanPostProcessor()`
Create a new `AutowiredAnnotationBeanPostProcessor` for Spring's standard `@Autowired` and `@Value` annotations.

## Method Summary

Modifier and Type
Method
Description
`Class<?>`
`determineBeanType(Class<?> beanClass, String beanName)`
Determine the type of the bean to be eventually returned from this processor's `InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class<?>, java.lang.String)` callback.
`Constructor<?>[]`
`determineCandidateConstructors(Class<?> beanClass, String beanName)`
Determine the candidate constructors to use for the given bean.
`protected boolean`
`determineRequiredStatus(org.springframework.core.annotation.MergedAnnotation<?> ann)`
Determine if the annotated field or method requires its dependency.
`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.
`BeanRegistrationAotContribution`
`processAheadOfTime(RegisteredBean registeredBean)`
Process the given `RegisteredBean` instance ahead-of-time and return a contribution or `null`.
`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.

### 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`

### Methods inherited from interface org.springframework.beans.factory.aot.BeanRegistrationAotProcessor

`isBeanExcludedFromAotProcessing`

### Methods inherited from interface org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor

`postProcessAfterInstantiation, postProcessBeforeInstantiation`

### Methods inherited from interface org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor

`getEarlyBeanReference, predictBeanType`

## Field Details

### logger

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

## Constructor Details

### AutowiredAnnotationBeanPostProcessor

public AutowiredAnnotationBeanPostProcessor()
Create a new `AutowiredAnnotationBeanPostProcessor` for Spring's standard `@Autowired` and `@Value` annotations.

Also supports the common `@Inject` annotation, if available, as well as the original `javax.inject.Inject` variant.

## Method Details

### 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 the common `@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 the common `@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.

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()

Specified by:
  `getOrder` in interface `org.springframework.core.Ordered`

### setBeanFactory

public void setBeanFactory(BeanFactory beanFactory)
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.
See Also:
  - `BeanInitializationException`

### postProcessMergedBeanDefinition

public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, 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
See Also:
  - `AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Class<?>, java.lang.String)`

### resetBeanDefinition

public void resetBeanDefinition(String beanName)
Description copied from interface: `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.

Specified by:
  `resetBeanDefinition` in interface `MergedBeanDefinitionPostProcessor`
Parameters:
  `beanName` - the name of the bean
See Also:
  - `DefaultListableBeanFactory.resetBeanDefinition(java.lang.String)`

### processAheadOfTime

@Nullable public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean)
Description copied from interface: `BeanRegistrationAotProcessor`
Process the given `RegisteredBean` instance ahead-of-time and return a contribution or `null`.

Processors are free to use any techniques they like to analyze the given instance. Most typically use reflection to find fields or methods to use in the contribution. Contributions typically generate source code or resource files that can be used when the AOT optimized application runs.

If the given instance isn't relevant to the processor, it should return a `null` contribution.

Specified by:
  `processAheadOfTime` in interface `BeanRegistrationAotProcessor`
Parameters:
  `registeredBean` - the registered bean to process
Returns:
  a `BeanRegistrationAotContribution` or `null`

### determineBeanType

public Class<?> determineBeanType(Class<?> beanClass, String beanName) throws BeanCreationException
Description copied from interface: `SmartInstantiationAwareBeanPostProcessor`
Determine the type of the bean to be eventually returned from this processor's `InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class<?>, java.lang.String)` callback.

The default implementation returns the given bean class as-is. Specific implementations should fully evaluate their processing steps in order to create/initialize a potential proxy class upfront.

Specified by:
  `determineBeanType` in interface `SmartInstantiationAwareBeanPostProcessor`
Parameters:
  `beanClass` - the raw class of the bean
  `beanName` - the name of the bean
Returns:
  the type of the bean (never `null`)
Throws:
  `BeanCreationException`

### determineCandidateConstructors

@Nullable public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeanCreationException
Description copied from interface: `SmartInstantiationAwareBeanPostProcessor`
Determine the candidate constructors to use for the given bean.

The default implementation returns `null`.

Specified by:
  `determineCandidateConstructors` in interface `SmartInstantiationAwareBeanPostProcessor`
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:
  `BeanCreationException`

### postProcessProperties

public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
Description copied from interface: `InstantiationAwareBeanPostProcessor`
Post-process the given property values before the factory applies them to the given bean.

The default implementation returns the given `pvs` as-is.

Specified by:
  `postProcessProperties` in interface `InstantiationAwareBeanPostProcessor`
Parameters:
  `pvs` - the property values that the factory is about to apply (never `null`)
  `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 the given bean (can be the passed-in PropertyValues instance), or `null` to skip property population

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

Parameters:
  `bean` - the target instance to process
Throws:
  `BeanCreationException` - if autowiring failed
See Also:
  - `setAutowiredAnnotationTypes(Set)`

### determineRequiredStatus

protected boolean determineRequiredStatus(org.springframework.core.annotation.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.

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