001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.beans.factory.annotation;
018
019import java.lang.annotation.Documented;
020import java.lang.annotation.ElementType;
021import java.lang.annotation.Retention;
022import java.lang.annotation.RetentionPolicy;
023import java.lang.annotation.Target;
024
025/**
026 * Marks a constructor, field, setter method, or config method as to be autowired by
027 * Spring's dependency injection facilities. This is an alternative to the JSR-330
028 * {@link javax.inject.Inject} annotation, adding required-vs-optional semantics.
029 *
030 * <h3>Autowired Constructors</h3>
031 * <p>Only one constructor of any given bean class may declare this annotation with the
032 * {@link #required} attribute set to {@code true}, indicating <i>the</i> constructor
033 * to autowire when used as a Spring bean. Furthermore, if the {@code required}
034 * attribute is set to {@code true}, only a single constructor may be annotated
035 * with {@code @Autowired}. If multiple <i>non-required</i> constructors declare the
036 * annotation, they will be considered as candidates for autowiring. The constructor
037 * with the greatest number of dependencies that can be satisfied by matching beans
038 * in the Spring container will be chosen. If none of the candidates can be satisfied,
039 * then a primary/default constructor (if present) will be used. Similarly, if a
040 * class declares multiple constructors but none of them is annotated with
041 * {@code @Autowired}, then a primary/default constructor (if present) will be used.
042 * If a class only declares a single constructor to begin with, it will always be used,
043 * even if not annotated. An annotated constructor does not have to be public.
044 *
045 * <h3>Autowired Fields</h3>
046 * <p>Fields are injected right after construction of a bean, before any config methods
047 * are invoked. Such a config field does not have to be public.
048 *
049 * <h3>Autowired Methods</h3>
050 * <p>Config methods may have an arbitrary name and any number of arguments; each of
051 * those arguments will be autowired with a matching bean in the Spring container.
052 * Bean property setter methods are effectively just a special case of such a general
053 * config method. Such config methods do not have to be public.
054 *
055 * <h3>Autowired Parameters</h3>
056 * <p>Although {@code @Autowired} can technically be declared on individual method
057 * or constructor parameters since Spring Framework 5.0, most parts of the
058 * framework ignore such declarations. The only part of the core Spring Framework
059 * that actively supports autowired parameters is the JUnit Jupiter support in
060 * the {@code spring-test} module (see the
061 * <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#testcontext-junit-jupiter-di">TestContext framework</a>
062 * reference documentation for details).
063 *
064 * <h3>Multiple Arguments and 'required' Semantics</h3>
065 * <p>In the case of a multi-arg constructor or method, the {@link #required} attribute
066 * is applicable to all arguments. Individual parameters may be declared as Java-8 style
067 * {@link java.util.Optional} or, as of Spring Framework 5.0, also as {@code @Nullable}
068 * or a not-null parameter type in Kotlin, overriding the base 'required' semantics.
069 *
070 * <h3>Autowiring Arrays, Collections, and Maps</h3>
071 * <p>In case of an array, {@link java.util.Collection}, or {@link java.util.Map}
072 * dependency type, the container autowires all beans matching the declared value
073 * type. For such purposes, the map keys must be declared as type {@code String}
074 * which will be resolved to the corresponding bean names. Such a container-provided
075 * collection will be ordered, taking into account
076 * {@link org.springframework.core.Ordered Ordered} and
077 * {@link org.springframework.core.annotation.Order @Order} values of the target
078 * components, otherwise following their registration order in the container.
079 * Alternatively, a single matching target bean may also be a generally typed
080 * {@code Collection} or {@code Map} itself, getting injected as such.
081 *
082 * <h3>Not supported in {@code BeanPostProcessor} or {@code BeanFactoryPostProcessor}</h3>
083 * <p>Note that actual injection is performed through a
084 * {@link org.springframework.beans.factory.config.BeanPostProcessor
085 * BeanPostProcessor} which in turn means that you <em>cannot</em>
086 * use {@code @Autowired} to inject references into
087 * {@link org.springframework.beans.factory.config.BeanPostProcessor
088 * BeanPostProcessor} or
089 * {@link org.springframework.beans.factory.config.BeanFactoryPostProcessor BeanFactoryPostProcessor}
090 * types. Please consult the javadoc for the {@link AutowiredAnnotationBeanPostProcessor}
091 * class (which, by default, checks for the presence of this annotation).
092 *
093 * @author Juergen Hoeller
094 * @author Mark Fisher
095 * @author Sam Brannen
096 * @since 2.5
097 * @see AutowiredAnnotationBeanPostProcessor
098 * @see Qualifier
099 * @see Value
100 */
101@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
102@Retention(RetentionPolicy.RUNTIME)
103@Documented
104public @interface Autowired {
105
106        /**
107         * Declares whether the annotated dependency is required.
108         * <p>Defaults to {@code true}.
109         */
110        boolean required() default true;
111
112}