@Qualifier annotation

background

        We know that when autowiring injection , you can use @Resource or @Autowired to inject beans

        But sometimes only a bean_id can't clearly identify the bean to be injected, so @Qualifier annotation can be introduced. In the class class and bean injection, @Qualifier() is added to achieve the effect of injecting a specific bean

 

1.  @Qualifier

        Examples are as follows:

//定义亚洲人
@Qualifier("Asian")
@Component
public class AsianMan extends Person {

}

········

//定义欧洲人
@Qualifier("Europe")
@Component
public class EuropeMan extends Person {

}

When injecting beans, you can use @Autowired to automatically inject 

public class Controller {

    @Qualifier("Asian")      //指定注入的是Asain的bean
    @Autowired
    private Person person;

}

You can also use xml configuration

<bean id="controller" class="org.leisu.Controller">
   <qualifier type="org.springframework.beans.factory.annotation.Qualifier" value="Asian"/>
</bean>

    It can be seen that when generating two beans that inherit Person, the characteristics of the two beans are specified through the @Qualifier() annotation, namely: "Asian", "Europe"; when injecting beans, the static type is the parent class Person, specify the injected bean as "Asian" via @Qualifier()

2. The bean already has an id, so why use @Qualifier?

      In the above example, we can set the ids of the two beans to "Asian" and "Europe" respectively, and then use @Resource(name="***") to achieve the same function, so why use @Qualifier ?

        Personally, I understand that @Qualifier is a modifier, and it does not need to be so strict that bean_id cannot be repeated, and it is more flexible. Then you can use the parameters as a modification, as an adjective of the bean for a more detailed description . In the "Spring in Action" book, for example, the classes IceCream (ice cream) and Dessert (dessert) are both decorated with @Qualifier("cold"), which means cold ice cream and dessert.

        因此使用@Qualifier,更加灵活,也更符合面向对象的思想。

2.  @Qualifier加在注解上

    上面的例子中,可以看到是使用@Qualifier()中value的参数来筛选bean的,本文给此参数起个名字,叫做修饰语。修饰语是字符串类型,可以设置各种值。

        如果一个工程的修饰语种类特别多,或者修饰语语义之间有继承等类似复杂关系,或者需要用多个修饰语修饰同一个bean,那用这种直接的方式就不太好操作了。修饰语value字符串容易拼错,容易混淆,更不容易被编辑器检测出来,调试bug困难。

        因此可以自定义一个新的注解,使用@Qualifier修饰,制作自己的修饰语注解

@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier("Asian")         //使用@Qualifier自定义修饰注解Asian
public @interface Asian{ ... }

···············

//定义亚洲人
@Asian          //使用我们自定义的修饰注解
@Component
public class AsianMan extends Person {

}

注入 

public class Controller {

    @Asian       //指定注入的是Asain的bean
    @Autowired
    private Person person;

}

 

        这种自定义注解数量不受限,便于统一管理,编辑器还能识别,因此推荐用这种方法来完成修饰与指定注入。

 转载于:https://blog.csdn.net/a909301740/article/details/78379720

@Autowired是根据类型进行自动装配的。如果当Spring上下文中存在不止一个UserDao类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存在UserDao类型的bean,也会抛出BeanCreationException异常。我们可以使用@Qualifier配合@Autowired来解决这些问题。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325756349&siteId=291194637