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接口的代碼如下:

 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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM