Spring使用注解實(shí)現(xiàn)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/beans https://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/> </beans>
如果在已有的配置文件中附加
在spring配置文件中引入context文件頭
xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
開啟屬性注解支持!
<context:annotation-config/>
二、@Autowired
@Autowired是按類型自動裝配的,不支持id匹配demo測試: 將User類中的set方法去掉,使用@Autowired注解
public class People { @Autowired private Cat cat; @Autowired private Dog dog; private String name; public Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } public String getName() { return name; } public void setName(String name) { this.name = name; }
此時配置文件內(nèi)容
<context:annotation-config/> <bean id="cat" class="com.lding.pojo.Cat"></bean> <bean id="dog" class="com.lding.pojo.Dog"></bean> <bean id="people" class="com.lding.pojo.People"></bean>
測試成功
@Autowired(required=false) false,對象可以為null;true,對象必須存對象,不能為null。
//如果允許對象為null,設(shè)置required = false,默認(rèn)為true @Autowired(required = false) private Cat cat;
三、@Qualifier
@Autowired是根據(jù)類型自動裝配的,加上@Qualifier則可以根據(jù)byName的方式自動裝配
@Qualifier不能單獨(dú)使用。
demo測試
配置文件修改內(nèi)容,保證類型存在對象。且名字不為類的默認(rèn)名字!
<bean id="dog1" class="com.lding.pojo.Dog"/> <bean id="dog2" class="com.lding.pojo.Dog"/> <bean id="cat1" class="com.lding.pojo.Cat"/> <bean id="cat2" class="com.lding.pojo.Cat"/>
沒有加Qualifier測試,直接報錯在屬性上添加Qualifier注解
@Autowired @Qualifier(value = "cat2") private Cat cat; @Autowired @Qualifier(value = "dog2") private Dog dog;
測試 ,成功輸出
四、@Resource
@Resource如有指定的name屬性,先按該屬性進(jìn)行byName方式查找裝配;
其次再進(jìn)行默認(rèn)的byName方式進(jìn)行裝配(默認(rèn)的byName方式就是查找set后屬性的小寫名字);
如果以上都不成功,則按byType的方式自動裝配
都不成功,則報異常。
demo測試
實(shí)體類
public class User { //如果允許對象為null,設(shè)置required = false,默認(rèn)為true @Resource(name = "cat2") private Cat cat; @Resource private Dog dog; private String str; }
beans.xml
<bean id="dog" class="com.lding.pojo.Dog"/> <bean id="cat1" class="com.lding.pojo.Cat"/> <bean id="cat2" class="com.lding.pojo.Cat"/> <bean id="user" class="com.lding.pojo.User"/>
測試:結(jié)果ok
配置文件2:beans.xml , 刪掉cat2
<bean id="dog" class="com.lding.pojo.Dog"/> <bean id="cat1" class="com.lding.pojo.Cat"/>
@Resource private Cat cat; @Resource private Dog dog;
結(jié)果:OK
結(jié)論:先進(jìn)行byName查找,失??;再進(jìn)行byType查找,成功。
總結(jié)
@Autowired與@Resource異同:
@Autowired與@Resource都可以用來裝配bean。都可以寫在字段上,或?qū)懺趕etter方法上。
@Autowired默認(rèn)按類型裝配(byType),如果我們想使用名稱裝配(byName)可以結(jié)合@Qualifier注解進(jìn)行使用
@Resource(屬于J2EE復(fù)返),默認(rèn)按照名稱進(jìn)行裝配,名稱可以通過name屬性進(jìn)行指定。如果沒有指定name屬性,當(dāng)注解寫在字段上時,默認(rèn)取字段名進(jìn)行按照名稱查找,如果注解寫在setter方法上默認(rèn)取屬性名進(jìn)行裝配。 當(dāng)找不到與名稱匹配的bean時才按照類型進(jìn)行裝配。但是需要注意的是,如果name屬性一旦指定,就只會按照名稱進(jìn)行裝配。
它們的作用相同都是用注解方式注入對象,但執(zhí)行順序不同。@Autowired先byType,@Resource先byName。
到此這篇關(guān)于Spring使用注解實(shí)現(xiàn)Bean的自動裝配的文章就介紹到這了,更多相關(guān)Spring Bean自動裝配內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實(shí)現(xiàn)本地文件存儲及預(yù)覽過程
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)本地文件存儲及預(yù)覽過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11java web服務(wù)器實(shí)現(xiàn)跨域訪問
這篇文章主要為大家詳細(xì)介紹了java web服務(wù)器實(shí)現(xiàn)跨域訪問,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08為什么Spring官方推薦的@Transational還能導(dǎo)致生產(chǎn)事故
在Spring中進(jìn)行事務(wù)管理非常簡單,只需要在方法上加上注解@Transactional,那么為什么Spring官方推薦的@Transational還能導(dǎo)致生產(chǎn)事故,本文就詳細(xì)的介紹一下2021-11-11Java防止頻繁請求、重復(fù)提交的操作代碼(后端防抖操作)
在客戶端網(wǎng)絡(luò)慢或者服務(wù)器響應(yīng)慢時,用戶有時是會頻繁刷新頁面或重復(fù)提交表單的,這樣是會給服務(wù)器造成不小的負(fù)擔(dān)的,同時在添加數(shù)據(jù)時有可能造成不必要的麻煩,今天通過本文給大家介紹下Java防止頻繁請求、重復(fù)提交的操作代碼,一起看看吧2022-04-04Java反應(yīng)式框架Reactor中的Mono和Flux
這篇文章主要介紹了Java反應(yīng)式框架Reactor中的Mono和Flux,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07