Class AutowiredAnnotationBeanPostProcessor

java.lang.Object
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
All Implemented Interfaces:
BeanRegistrationAotProcessor, Aware, BeanFactoryAware, BeanPostProcessor, InstantiationAwareBeanPostProcessor, SmartInstantiationAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor, Ordered, PriorityOrdered

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:
  • Field Details

    • logger

      protected final 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