org.springframework.beans.factory.annotation

Annotation Type Autowired



  • @Target(value={CONSTRUCTOR,METHOD,PARAMETER,FIELD,ANNOTATION_TYPE})
     @Retention(value=RUNTIME)
     @Documented
    public @interface Autowired
    Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.

    Only one constructor (at max) of any given bean class may carry this annotation, indicating the constructor to autowire when used as a Spring bean. Such a 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.

    In the case of a multi-arg constructor or method, the 'required' parameter is applicable to all arguments. Individual parameters may be declared as Java-8-style Optional or, as of Spring Framework 5.0, also as @Nullable or a not-null parameter type in Kotlin, overriding the base required semantics.

    In case of a Collection or Map dependency type, the container autowires all beans matching the declared value type. For such purposes, the map keys must be declared as type String which will be resolved to the corresponding bean names. Such a container-provided collection will be ordered, taking into account Ordered/Order values of the target components, otherwise following their registration order in the container. Alternatively, a single matching target bean may also be a generally typed Collection or Map itself, getting injected as such.

    Note that actual injection is performed through a BeanPostProcessor which in turn means that you cannot use @Autowired to inject references into BeanPostProcessor or BeanFactoryPostProcessor types. Please consult the javadoc for the AutowiredAnnotationBeanPostProcessor class (which, by default, checks for the presence of this annotation).

    Since:
    2.5
    Author:
    Juergen Hoeller, Mark Fisher
    See Also:
    AutowiredAnnotationBeanPostProcessor, Qualifier, Value
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element and Description
      boolean required
      Declares whether the annotated dependency is required.
    • Element Detail

      • required

        public abstract boolean required
        Declares whether the annotated dependency is required.

        Defaults to true.

        Default:
        true