今天来跟大家聊聊简单聊聊@Autowired,Autowired翻译过来为自动装配,也就是自动给Bean对象的属性赋值。
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD,
ElementType.PARAMETER, ElementType.FIELD,
ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
/**
* Declares whether the annotated dependency is required.
* <p>Defaults to {@code true}.
*/
booleanrequired() defaulttrue;
}
复制代码
以上是@Autowired的定义,重点看 @Target,我们发现@Autowired可以写在:
ElementType.CONSTRUCTOR:表示可以写在构造方法上