首先解释一下@Primary和@Qualifier这两个注解的意思:@Primary的意思是在众多相同的bean中,优先使用用@Primary注解的bean.而@Qualifier这个注解则指定某个bean有没有资格进行注入。
示例代码的思路是:1.一个接口Dessert和这个接口的三个实现类,2.再在一个类(AbrahamLincoln)中自动注入Dessert这个接口,3.用自动扫描机制自动创建bean.
如果不用@Primary和@Qualifier注解,势必出现如下错误:NoUniqueBeanDefinitionException.
示例代码如下:【用@Primary来解决问题】
Dessert接口的代码如下:
1 package com.advancedWiring.ambiguityInAutowiring;
2
3 import org.springframework.stereotype.Component;
4
5 /**
6 * Created by ${秦林森} on 2017/6/9.
7 */
8 @Component
9 public interface Dessert {
10 void amI();
11 }
Cake的代码如下:
1 package com.advancedWiring.ambiguityInAutowiring;
2
3 import org.springframework.beans.factory.annotation.Qualifier;
4 import org.springframework.context.annotation.Primary;
5 import org.springframework.stereotype.Component;
6
7 /**
8 * Created by ${秦林森} on 2017/6/9.
9 */
10 @Component
11 public class Cake implements Dessert {
12 @Override
13 public void amI() {
14 System.out.println("I am cake");
15 }
16 }
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的代码如下:
1 package com.advancedWiring.ambiguityInAutowiring;
2
3 import org.springframework.context.annotation.ComponentScan;
4 import org.springframework.context.annotation.Configuration;
5
6 /**
7 * Created by ${秦林森} on 2017/6/9.
8 */
9 @Configuration
10 /**
11 * 用@ComponentScan自动扫描创建bean
12 */
13 @ComponentScan(basePackageClasses = Dessert.class)
14 public class DessertConfig {
15
16 }
测试类的代码如下:
1 package com.advancedWiring.ambiguityInAutowiring;
2
3 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4
5 /**
6 * Created by ${秦林森} on 2017/6/9.
7 */
8 public class AmbiguityTest {
9 public static void main(String[] args) {
10 AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(DessertConfig.class);
11 Dessert dessert = ac.getBean(Dessert.class);
12 dessert.amI();
13 }
14 }
二:用@Qualifier这个注解来解决问题:
核心代码如下:
1 package com.advancedWiring.ambiguityInAutowiring;
2
3 import org.springframework.beans.factory.annotation.Qualifier;
4 import org.springframework.context.annotation.Primary;
5 import org.springframework.stereotype.Component;
6
7
8 /**
9 * Created by ${秦林森} on 2017/6/9.
10 */
11 @Component
12 @Qualifier("crispy")
13 public class Cookie implements Dessert {
14 @Override
15 public void amI() {
16 System.out.println("I am cookie");
17 }
18 }
1 package com.advancedWiring.ambiguityInAutowiring;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.beans.factory.annotation.Qualifier;
5 import org.springframework.stereotype.Component;
6
7 /**
8 * Created by ${秦林森} on 2017/6/9.
9 */
10 @Component
11 public class AbrahamLincoln {
12 private Dessert dessert;
13
14 public Dessert getDessert() {
15 return dessert;
16 }
17 @Autowired
18 @Qualifier("crispy")
19 public void setDessert(Dessert dessert) {
20 this.dessert = dessert;
21 }
22 }