7.10.8 Providing qualifier metadata with annotations

第一种 传统使用方法

    @Bean
    @Qualifier("stu")
    public Student student() {
        Student student = new Student();

        return student;
    }
    @Autowired
    @Qualifier("stu")
    private Student stu;

第二种

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier("strongStu")
public @interface StrongStu {
}
@Bean
@StrongStu
public Student studentStr() {
    Student student = new Student();

    return student;
}
@Autowired
@StrongStu
private  Student stu;

第三种

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Deng {
    String value() default "";
}
@Bean
@Deng("dengyi")
public Student studentDengyi() {
    Student student = new Student();

    return student;
}
@Autowired
@Deng("dengyi")
private Student student;

results matching ""

    No results matching ""