溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用@Autowierd

發(fā)布時間:2021-08-17 09:10:05 來源:億速云 閱讀:150 作者:小新 欄目:開發(fā)技術

這篇文章主要介紹了如何使用@Autowierd,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

@Autowierd(自動裝配)的使用

@Autowired 是一個注釋,它可以對類成員變量、方法及構造函數進行標注,讓 spring 完成 bean 自動裝配的工作。

一、介紹@Autowierd自動裝配之前我們需要先了解何為裝配?

首先我們來看最原生態(tài)的裝配,以一個人分別養(yǎng)了貓和狗為例,我們先分別為貓和狗進行實例化:

<bean id="cat" class="com.spring05.pojo.Cat"/>
    <bean id="dog" class="com.spring05.pojo.Dog"/>

由于person類的屬性中帶有貓和狗,所以我們需要將貓和狗的實體類注入人的實體類中:

<bean id = "Person" class="com.spring05.pojo.Person">
        <property name="dog" ref="dog"/>
        <property name="cat" ref="cat"/>
    </bean>

以上就是裝配,所謂的屬性注入

但是我們知道,如果是手動注入的屬性的話,一旦屬性數量多的話會顯得很繁瑣,這時候自動裝配的作用就體現(xiàn)出來了

二、@Autowierd自動裝配的使用

第一步,使用@Autowierd注釋需要在配置文件中開啟注解支持

<!--開啟注解的支持-->
        <context:annotation-config/>

但是相應的需要在配置文件中加入context約束:

xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd

接下來就是注解的使用了,@Autowierd注釋的使用只需要在Person類中的屬性上加上一個@Autowierd注釋即可實現(xiàn)自動裝配

@Autowired
    private Cat cat;
    @Autowired
    private Dog dog;

自動裝配完了之后在spring容器中注冊person類時就不需要在對person類的bean添加屬性注入,這邊放入整個配置文件以供參考

<?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/context
        http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
 
        <!--開啟注解的支持-->
        <context:annotation-config/>
    <bean id="cat" class="com.spring05.pojo.Cat"/>
    <bean id="dog" class="com.spring05.pojo.Dog"/>
    <bean id="Person" class="com.spring05.pojo.Person"/>
</beans>

除了@Autowierd之外還需要介紹@Resource注釋,@Resource注釋與@Autowierd功能相同,@Resource甚至包括了@Autowierd

三、使用注解@Autowierd的"搭檔"@Qualifier

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

@Autowired
    @Qualifier(value = "cat")
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog")
    private Dog dog;

四、注意事項

1、使用Autowired我們可以省略set方法,但是使用注解的前提是裝配的屬性必須在IOC容器中存在,且符合名字byname

2、如果定義了@Autowired的required屬性為false,說明這個對象可以為空,否則不允許為空:

@Autowired(required = false)

3、不僅僅只有通過注釋可以自動裝配,還可以通過ByName和ByType來自動裝配:

<bean id="Person" class="com.spring05.pojo.Person" autowire="byType"/>
    <bean id="Person" class="com.spring05.pojo.Person" autowire="byName"/>

SpringBoot的Autowierd失敗

通常是以下幾種可能:

1.沒有加@Service注解,或者是這個bean沒有放在標注了@Configuration這個注解的類下。

2.SpringBoot啟動類沒有開啟掃描

@ComponentScan(value = {"com.bihang"})

感謝你能夠認真閱讀完這篇文章,希望小編分享的“如何使用@Autowierd”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI