spring in action 学习笔记八:用@Primary 或者@Qualifier消除@Autowired引起的歧义现象

  首先解释一下@Primary和@Qualifier这两个注解的意思:@Primary的意思是在众多相同的bean中,优先使用用@Primary注解的bean.而@Qualifier这个注解则指定某个bean有没有资格进行注入。

示例代码的思路是:1.一个接口Dessert和这个接口的三个实现类,2.再在一个类(AbrahamLincoln)中自动注入Dessert这个接口,3.用自动扫描机制自动创建bean.

如果不用@Primary和@Qualifier注解,势必出现如下错误:NoUniqueBeanDefinitionException.

  示例代码如下:【用@Primary来解决问题】

Dessert接口的代码如下:

 package com.advancedWiring.ambiguityInAutowiring;

 import org.springframework.stereotype.Component;

 /**
* Created by ${秦林森} on 2017/6/9.
*/
@Component
public interface Dessert {
void amI();
}

Cake的代码如下:

 package com.advancedWiring.ambiguityInAutowiring;

 import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Component
public class Cake implements Dessert {
@Override
public void amI() {
System.out.println("I am cake");
}
}

Cookie的代码如下:

package com.advancedWiring.ambiguityInAutowiring;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Component
public class Cookie implements Dessert {
@Override
public void amI() {
System.out.println("I am cookie");
}
}

IceCream的代码如下:

package com.advancedWiring.ambiguityInAutowiring;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Component
@Primary//注意这里用了@Primary这个注解。
public class IceCream implements Dessert{
@Override
public void amI() {
System.out.println("I am ice cream");
}
}

DessertConfig的代码如下:

 package com.advancedWiring.ambiguityInAutowiring;

 import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Configuration
/**
* 用@ComponentScan自动扫描创建bean
*/
@ComponentScan(basePackageClasses = Dessert.class)
public class DessertConfig { }

测试类的代码如下:

 package com.advancedWiring.ambiguityInAutowiring;

 import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 /**
* Created by ${秦林森} on 2017/6/9.
*/
public class AmbiguityTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(DessertConfig.class);
Dessert dessert = ac.getBean(Dessert.class);
dessert.amI();
}
}

二:用@Qualifier这个注解来解决问题:

核心代码如下:

 package com.advancedWiring.ambiguityInAutowiring;

 import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Component
@Qualifier("crispy")
public class Cookie implements Dessert {
@Override
public void amI() {
System.out.println("I am cookie");
}
}
 package com.advancedWiring.ambiguityInAutowiring;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Component
public class AbrahamLincoln {
private Dessert dessert; public Dessert getDessert() {
return dessert;
}
@Autowired
@Qualifier("crispy")
public void setDessert(Dessert dessert) {
this.dessert = dessert;
}
}
上一篇:php小知识点


下一篇:从头开始搭建分布式日志平台的docker环境