Top Description Inners Fields Constructors Methods
org.springframework.beans.factory.annotation

public Class AutowiredAnnotationBeanPostProcessor

extends Object
implements SmartInstantiationAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor, BeanRegistrationAotProcessor, PriorityOrdered, BeanFactoryAware
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.Aware, org.springframework.core.PriorityOrdered, org.springframework.core.Ordered, org.springframework.beans.factory.aot.BeanRegistrationAotProcessor, org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor, org.springframework.beans.factory.config.BeanPostProcessor, org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor, org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor
Imports
java.beans.PropertyDescriptor, java.io.IOException, java.lang.annotation.Annotation, java.lang.reflect.AccessibleObject, .Constructor, .Field, .InvocationTargetException, .Member, .Method, .Modifier, java.util.ArrayList, .Arrays, .Collection, .Collections, .Iterator, .LinkedHashSet, .List, .Map, .Set, java.util.concurrent.ConcurrentHashMap, org.apache.commons.logging.Log, .LogFactory, org.springframework.aot.generate.AccessControl, .GeneratedClass, .GeneratedMethod, .GenerationContext, org.springframework.aot.hint.ExecutableMode, .RuntimeHints, org.springframework.aot.hint.support.ClassHintUtils, org.springframework.beans.BeanUtils, .BeansException, .PropertyValues, .TypeConverter, org.springframework.beans.factory.BeanCreationException, .BeanFactory, .BeanFactoryAware, .InjectionPoint, .NoSuchBeanDefinitionException, .UnsatisfiedDependencyException, org.springframework.beans.factory.aot.AutowiredArgumentsCodeGenerator, .AutowiredFieldValueResolver, .AutowiredMethodArgumentsResolver, .BeanRegistrationAotContribution, .BeanRegistrationAotProcessor, .BeanRegistrationCode, org.springframework.beans.factory.config.ConfigurableListableBeanFactory, .DependencyDescriptor, .SmartInstantiationAwareBeanPostProcessor, org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory, .AutowireCandidateResolver, .DefaultListableBeanFactory, .LookupOverride, .MergedBeanDefinitionPostProcessor, .RegisteredBean, .RootBeanDefinition, org.springframework.core.BridgeMethodResolver, .MethodParameter, .Ordered, .PriorityOrdered, org.springframework.core.annotation.AnnotationUtils, .MergedAnnotation, .MergedAnnotations, org.springframework.core.type.AnnotationMetadata, .MethodMetadata, org.springframework.core.type.classreading.MetadataReaderFactory, .SimpleMetadataReaderFactory, org.springframework.javapoet.ClassName, .CodeBlock, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .ObjectUtils, .ReflectionUtils, .StringUtils

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.

Authors
Juergen Hoeller, Mark Fisher, Stephane Nicoll, Sebastien Deleuze, Sam Brannen, Phillip Webb
Since
2.5
See Also
setAutowiredAnnotationType, Autowired, Value

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private abstract static class
AutowiredAnnotationBeanPostProcessor.AutowiredElement

Base class representing injection information.

private class
AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement

Class representing injection information about an annotated field.

private class
AutowiredAnnotationBeanPostProcessor.AutowiredMethodElement

Class representing injection information about an annotated method.

private static class
AutowiredAnnotationBeanPostProcessor.ShortcutDependencyDescriptor

DependencyDescriptor variant with a pre-resolved target bean name.

Field Summary

Modifier and TypeField and Description
private final Set<Class<? extends Annotation>>
private ConfigurableListableBeanFactory
private final Map<Class<?>, Constructor<?>[]>
private static final Constructor<?>[]
private final Map<String, InjectionMetadata>
protected final Log
private final Set<String>
private MetadataReaderFactory
private int
private String
private boolean

Constructor Summary

AccessConstructor and Description
public
AutowiredAnnotationBeanPostProcessor()

Create a new AutowiredAnnotationBeanPostProcessor for Spring's standard @Autowired and @Value annotations.

Method Summary

Modifier and TypeMethod and Description
private InjectionMetadata
private void
checkLookupMethods(Class<?> beanClass, final String beanName)

public Class<?>
determineBeanType(Class<?>
the raw class of the bean
beanClass
,
String
the name of the bean
beanName
)

Overrides default org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor.determineBeanType.

Determine the type of the bean to be eventually returned from this processor's postProcessBeforeInstantiation callback.

public Constructor<?>[]
determineCandidateConstructors(Class<?>
the raw class of the bean (never null)
beanClass
,
final String
the name of the bean
beanName
)

Overrides default org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor.determineCandidateConstructors.

Determine the candidate constructors to use for the given bean.

protected boolean

Returns:

whether the annotation indicates that a dependency is required
determineRequiredStatus
(MergedAnnotation<?>
the Autowired annotation
ann
)

Determine if the annotated field or method requires its dependency.

private MergedAnnotation<?>
private InjectionMetadata
private InjectionMetadata
findInjectionMetadata(String beanName, Class<?> beanType, RootBeanDefinition beanDefinition)

private AutowireCandidateResolver
private Collection<AutowiredAnnotationBeanPostProcessor.AutowiredElement>
public int
getOrder()

Implements org.springframework.core.Ordered.getOrder.

Get the order value of this object.

public void
postProcessMergedBeanDefinition(RootBeanDefinition
the merged bean definition for the bean
beanDefinition
,
Class<?>
the actual type of the managed bean instance
beanType
,
String
the name of the bean
beanName
)

Implements org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition.

Post-process the given merged bean definition for the specified bean.

public PropertyValues
postProcessProperties(PropertyValues
the property values that the factory is about to apply (never null)
pvs
,
Object
the bean instance created, but whose properties have not yet been set
bean
,
String
the name of the bean
beanName
)

Overrides default org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessProperties.

Post-process the given property values before the factory applies them to the given bean.

public BeanRegistrationAotContribution
processAheadOfTime(RegisteredBean
the registered bean to process
registeredBean
)

Implements org.springframework.beans.factory.aot.BeanRegistrationAotProcessor.processAheadOfTime.

Process the given RegisteredBean instance ahead-of-time and return a contribution or null.

public void
processInjection(Object
the target instance to process
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.

private void
registerDependentBeans(String beanName, Set<String> autowiredBeanNames)

Register the specified bean as dependent on the autowired beans.

public void
resetBeanDefinition(String
the name of the bean
beanName
)

Overrides default org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor.resetBeanDefinition.

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.

private Object
resolveCachedArgument(String beanName, Object cachedArgument)

Resolve the specified cached method argument or field value.

public void
setAutowiredAnnotationType(Class<? extends Annotation> autowiredAnnotationType)

Set the 'autowired' annotation type, to be used on constructors, fields, setter methods, and arbitrary config methods.

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.

public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.

public void
setOrder(int order)

public void
setRequiredParameterName(String requiredParameterName)

Set the name of an attribute of the annotation that specifies whether it is required.

public void
setRequiredParameterValue(boolean requiredParameterValue)

Set the boolean value that marks a dependency as required.

private List<InjectionMetadata.InjectedElement>
sortMethodElements(List<InjectionMetadata.InjectedElement> methodElements, Class<?> targetClass)

Sort the method elements via ASM for deterministic declaration order if possible.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait