今天来跟大家聊聊简单聊聊@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:表示可以写在构造方法上
ElementType.METHOD:表示可以写在普通方法上

本文探讨了Spring框架中的@Autowired注解,包括它可以应用于构造方法、普通方法、方法参数、属性以及自定义注解上。详细阐述了各场景下的工作原理,如构造方法的选择逻辑,方法参数的特殊性以及属性自动装配的默认行为。
最低0.47元/天 解锁文章
868

被折叠的 条评论
为什么被折叠?



