Spring常用注解总结(2)
@Autowired
“自动填装”,作用是为了消除代码JAVA代码里面的getter/setter与bean属性中的property。
@Autowired默认按类型匹配的方式,在容器查找匹配的Bean,当仅有一个匹配的Bean时,Spring将其注入@Autowired标注的变量中。
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
boolean required() default true;
}
@Qualifier
在容器中有一个以上匹配的Bean,则可以通过@Qualifier注解限定Bean的名称,来调用对应的Bean对象
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Qualifier {
String value() default "";
}
@Configurable
有时,我们需要将一些依赖注入到非Spring管理的类型中(例如在实体类中注入服务或者DAO层)。这时我们希望该类被Spring管理,可以使用@Configurable来帮助我们实现。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Configurable {
String value() default "";
Autowire autowire() default Autowire.NO;
boolean dependencyCheck() default false;
boolean preConstruction() default false;
}
未完待续,积累点点滴滴,一步一脚印,加油

更多精彩