Annotation Type Autowired
@Target({CONSTRUCTOR,METHOD,PARAMETER,FIELD,ANNOTATION_TYPE}) @Retention(RUNTIME) @Documented public @interface Autowired
Marks a constructor, field, setter method, or config method as to be autowired by Spring's dependency injection facilities. This is an alternative to the JSR-330Injectannotation, adding required-vs-optional semantics.Autowired Constructors
Only one constructor of any given bean class may declare this annotation with the
required()attribute set totrue, indicating the constructor to autowire when used as a Spring bean. Furthermore, if therequiredattribute is set totrue, 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. Similarly, if a class declares multiple constructors but none of them is annotated with@Autowired, 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. Such config methods do not have to be public.
Autowired Parameters
Although
@Autowiredcan technically be declared on individual method or constructor parameters since Spring Framework 5.0, most parts of the framework ignore such declarations. The only part of the core Spring Framework that actively supports autowired parameters is the JUnit Jupiter support in thespring-testmodule (see the TestContext framework reference documentation for details).Multiple Arguments and 'required' Semantics
In the case of a multi-arg constructor or method, the
required()attribute is applicable to all arguments. Individual parameters may be declared as Java-8 styleOptionalor, as of Spring Framework 5.0, also as@Nullableor a not-null parameter type in Kotlin, overriding the base 'required' semantics.Autowiring Arrays, Collections, and Maps
In case of an array,
Collection, orMapdependency type, the container autowires all beans matching the declared value type. For such purposes, the map keys must be declared as typeStringwhich will be resolved to the corresponding bean names. Such a container-provided collection will be ordered, taking into accountOrderedand@Ordervalues of the target components, otherwise following their registration order in the container. Alternatively, a single matching target bean may also be a generally typedCollectionorMapitself, getting injected as such.Not supported in
BeanPostProcessororBeanFactoryPostProcessorNote that actual injection is performed through a
BeanPostProcessorwhich in turn means that you cannot use@Autowiredto inject references intoBeanPostProcessororBeanFactoryPostProcessortypes. Please consult the javadoc for theAutowiredAnnotationBeanPostProcessorclass (which, by default, checks for the presence of this annotation).- Since:
- 2.5
- Author:
- Juergen Hoeller, Mark Fisher, Sam Brannen
- See Also:
AutowiredAnnotationBeanPostProcessor,Qualifier,Value
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description booleanrequiredDeclares whether the annotated dependency is required.
Element Detail
required
boolean required
Declares whether the annotated dependency is required.Defaults to
true.- Default:
- true