Spring@Autowried注解和@Qualifier注解


今天學習了Spring的@Autowried注解,來寫一篇博客來回想一下。

@Autowried注解是干什么的呢?
它可以對類成員變量、方法及構造函數進行標注,完成自動裝配的工作。 通過 @Autowired的使用來消除 set ,get方法。

在沒有用@Autowried注解之前的時候,我們對一個bean配置屬性的時候,通常是這樣的。

      <property name="xxx" value="xxx"></property>

如果配置的屬性比較多的時候,這中方式就比較繁瑣,所以在Spring 2.5 引入了 @Autowired 注釋

代碼實現:

      //人類
      public class Person {
          private Dog dog;
          private Cat cat;

          public Dog getDog() {
              return dog;
          }

          public void setDog(Dog dog) {
              this.dog = dog;
          }

          public Cat getCat() {
              return cat;
          }

          public void setCat(Cat cat) {
              this.cat = cat;
          }
      }


      //貓類
    public class Cat implements animal {

          public void shoot() {
              System.out.println("miao~");
          }
      }
      
      //動物接口
     public interface animal {
              //叫的方法
          void shoot();
      }

      public class Dog implements animal{

          public void shoot() {
              System.out.println("giao~");
          }
      }
        



我們實現了一個人的類,他有一條狗和一只貓。並且都實現了shoot方法。
我們之前沒有用注解的時候,配置文件要這樣寫:

      <bean id="cat" class="com.gaoteng.pojo.Cat"></bean>
    <bean id="dog" class="com.gaoteng.pojo.Dog"></bean>
    <bean id="person" class="com.gaoteng.pojo.Person">
        <property name="cat" ref="cat"></property>
        <property name="dog" ref="dog"></property>
    </bean>

使用@Autowried注解后:

      <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
    <!--開啟對注解的支持-->
    <context:annotation-config/>
    <bean id="cat" class="com.gaoteng.pojo.Cat"></bean>
    <bean id="dog" class="com.gaoteng.pojo.Dog"></bean>
    <bean id="person" class="com.gaoteng.pojo.Person"></bean>
</beans>

      public class Person {
    @Autowired
    private Dog dog;
    @Autowired
    private Cat cat;

    public Dog getDog() {
        return dog;
    }
    public Cat getCat() {
        return cat;
    }
    
}

我們只需要對person類中的成員變量(dog和cat)進行標識即可.
來測試一下:

      @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Person person = context.getBean("person", Person.class);
        person.getDog().shoot();
        person.getCat().shoot();
    }


注意:
查閱資料發現:在使用@Autowired注解時,默認使用ByType(通過類型)來進行查找,如果沒有查詢到,拋出異常
如果查詢結果為一個時,就將該bean裝配給@Autowired指定的數據
如果查詢結果為多個時,@Autowried注解會根據SetXxx中的xxx來查找(ByName通過名字)。
(如果查詢到,就將該bean裝配給@Autowired指定的數據.如果沒有查詢到,拋出異常。)

舉例說明:

            <!--開啟對注解的支持-->
    <context:annotation-config/>
    <bean id="cat" class="com.gaoteng.pojo.Cat"></bean>
    <bean id="cat2" class="com.gaoteng.pojo.Cat"></bean>
    <bean id="dog" class="com.gaoteng.pojo.Dog"></bean>
    <bean id="dog2" class="com.gaoteng.pojo.Dog"></bean>
    <bean id="person" class="com.gaoteng.pojo.Person"></bean>

我們發現有多個查詢結果.測試代碼不變
測試一下:

我們可以看到結果並沒有發生改變,說明如果查詢結果為多個時,@Autowried注解會根據SetXxx中的xxx來查找。

在來一個栗子:

            <!--開啟對注解的支持-->
    <context:annotation-config/>
    <bean id="cat3" class="com.gaoteng.pojo.Cat"></bean>
    <bean id="cat2" class="com.gaoteng.pojo.Cat"></bean>
    <bean id="dog3" class="com.gaoteng.pojo.Dog"></bean>
    <bean id="dog2" class="com.gaoteng.pojo.Dog"></bean>
    <bean id="person" class="com.gaoteng.pojo.Person"></bean>

我們發現同樣有多個查詢結果.測試代碼不變
測試一下:

我們發現發生了異常,我們來截取異常的信息

Unsatisfied dependency expressed through field 'dog'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.gaoteng.pojo.Dog' available: expected single matching bean but found 2: dog2,dog3

翻譯一下就是:
通過字段“ dog”表示不滿意的依賴關系; 嵌套的異常是org.springframework.beans.factory.NoUniqueBeanDefinitionException:沒有類型為'com.gaoteng.pojo.Dog'的合格bean:期望的單個匹配bean,但找到2:dog2,dog3.

如果@Autowried自動裝配環境比較復雜時,自動裝配無法通過一個@Autowired注解完成時,我們可以使用@Qualifier(value = "xxx")注解配合@Autowired使用,來指定一個唯一的bean對象的注入.

    public class Person {
    @Autowired
    @Qualifier(value = "dog2")
    private Dog dog;
    @Autowired
    @Qualifier(value = "cat2")
    private Cat cat;

    public Dog getDog() {
        return dog;
    }
    public Cat getCat() {
        return cat;
    }

}

配置文件

      <!--開啟對注解的支持-->
    <context:annotation-config/>
    <bean id="cat2" class="com.gaoteng.pojo.Cat"></bean>
    <bean id="dog2" class="com.gaoteng.pojo.Dog"></bean>
    <bean id="person" class="com.gaoteng.pojo.Person"></bean>

測試代碼

     @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Person person = context.getBean("person", Person.class);
        person.getDog().shoot();
        person.getCat().shoot();
    }

來測試一下:

與@Autowried注解作用很像的一個注解@Resource

@Resource默認按照名稱(xxx)進行查找,找不到按類型.

      public class Person {
    @Resource
    private Dog dog;
    @Resource
    private Cat cat;

    public Dog getDog() {
        return dog;
    }
    public Cat getCat() {
        return cat;
    }

}

本篇博客參考以下博客:
@Autowired用法詳解
在此特別感謝!


免責聲明!

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



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