Spring中的自動裝配機(jī)制詳解
一、什么是自動裝配?
自動裝配就是會通過Spring的上下文為你找出相應(yīng)依賴項(xiàng)的類,通俗的說就是Spring會在上下文中自動查找,并自動給Bean裝配與其相關(guān)的屬性!
spring中實(shí)現(xiàn)自動裝配的方式有兩種,一種是通過xml文件、一種是通過注解的方式。
下面為大家介紹這兩種方式實(shí)現(xiàn)自動裝配。
為了更簡單的讓大家理解,我們通過例子來說明: 有以下三個(gè)實(shí)體類,People類,Dog類,Cat類,分別代表人、狗、貓。
人養(yǎng)了一只狗和一只貓,貓和狗都會叫。
public class Cat { public void shout(){ System.out.println("miao~"); } }
public class Dog { public void shout(){ System.out.println("wang wang~"); } }
public class Peopel { private Cat cat; private Dog dog; private String name; Setter/Getter...
二、手動裝配
講自動裝配之前,我們先來說一下手動裝配,手動裝配又是什么?
手動裝配就是通過xml配置自己將bean中所關(guān)聯(lián)的其他bean裝配進(jìn)去。
用代碼表示:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="cat" class="com.kuang.pojo.Cat"/> <bean id="dog" class="com.kuang.pojo.Dog"/> <bean id="people" class="com.kuang.pojo.Peopel"> <property name="name" value="張三"/> <property name="cat" ref="cat"/> <property name="dog" ref="dog"/> </bean></beans>
在id=people的bean(以后id=xx的bean我們就叫xxBean)中,我們給peopleBean手動裝配了與之關(guān)聯(lián)的catBean和dogBean,這就叫做手動裝配。
那么有沒有什么辦法,我們可以不用去手動裝配關(guān)聯(lián)的bean,讓spring幫我們自動把關(guān)聯(lián)的bean裝配進(jìn)去呢?答案是肯定的。
自動裝配就可以幫助我們解決這個(gè)問題。
實(shí)現(xiàn)自動裝配有兩種方式。一種是使用注解的方式、另一種是通過xml文件的方式。
下面我們倆講實(shí)現(xiàn)自動裝配的兩種方式。
三、自動裝配
3.1 通過xml文件實(shí)現(xiàn)自動裝配
我們只需要在xml配置文件中的bean標(biāo)簽中加入一個(gè)屬性autowire即可,例如:
<bean id="people" class="com.kuang.pojo.Peopel" autowire="byName"> <property name="name" value="張三"/> </bean>
使用autowire關(guān)鍵字聲明bean的自動裝配方式。
其可選值為byName、byType、constructor,default,no;
這里講前邊兩個(gè)。
3.1.1 byName
設(shè)置autowire屬性為byName,那么Spring會根據(jù)class屬性找到實(shí)體類,然后查詢實(shí)體類中所有setter方法的名字,根據(jù)setter方法后面的名字(例如SetDog,則setter方法后面的名字為dog)再到配置文件中尋找一個(gè)與該名字相同id的Bean,注入進(jìn)來。如圖:
3.1.2 byType
設(shè)置autowire屬性為byType,那么Spring會自動尋找一個(gè)與該屬性類型相同的Bean,注入進(jìn)來。
*注意:使用byType這種方式,必須保證配置文件中所有bean的class屬性的值是唯一的,否則就會報(bào)錯
例如:下邊這種方式是錯誤的,因?yàn)閮蓚€(gè)bean中的class屬性的值重復(fù)了,會報(bào)錯
3.2 通過注解實(shí)現(xiàn)自動裝配
注解是通過反射來實(shí)現(xiàn)的。
3.2.1 使用注解前的準(zhǔn)備:
<?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>
注意: conteext:annotation-config/ 必須要寫在xml中,這是用來開啟注解的支持,如果不加上注解就無效。
3.2.2 使用
首先xml配置文件如下:
<?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/> <bean id="cat" class="com.kuang.pojo.Cat"/> <bean id="dog" class="com.kuang.pojo.Dog"/> <bean id="people" class="com.kuang.pojo.Peopel"> <property name="name" value="張三"/> </bean> </beans>
然后在實(shí)體類的對應(yīng)屬性上添加@Autowired注解(也可以把注解放到對應(yīng)屬性的setter上),people類中依賴Dog類和Cat類。
所以在people類中的dog和cat屬性上要加上@Autowired,實(shí)現(xiàn)自動裝配。
public class Peopel { @Autowired private Cat cat; @Autowired private Dog dog; private String name; setter/getter... }
3.3 重點(diǎn)
(1)注解方法裝配屬性的過程:spring會默認(rèn)優(yōu)先根據(jù)(被注解修飾的)屬性類型去容器中找對應(yīng)的組件(bean),找到就賦值;若找到多個(gè)相同類型的組件,再將屬性的名稱作為組件(bean)的id去容器中查找。
(2)@Qualifier注解可以和使用Autowired搭配使用:@Qualifier指定需要裝配的組件的id,而不是使用屬性名。
例如下邊例子,spring就會優(yōu)先在容器中查找id為“cat”的組件。
public class Peopel { @Autowired @Qualifier(value = "cat") private Cat cat; }
什么情況會使用到@Qualifier注解:當(dāng)ioc容器根據(jù)屬性類型去容器中找找到多個(gè)相同類型的組件,再將屬性的名稱作為組件(bean)的id去容器中查找找不到時(shí)就是用這兩個(gè)注解搭配,指定需要裝配的bean的id。
(3)在默認(rèn)情況下使用 @Autowired 注釋進(jìn)行自動注入時(shí),Spring 容器中匹配的候選 Bean 數(shù)目必須有且僅有一個(gè)。當(dāng)找不到一個(gè)匹配的 Bean 時(shí),Spring 容器將拋出 BeanCreationException 異常,并指出必須至少擁有一個(gè)匹配的 Bean。當(dāng)不能確定 Spring 容器中一定擁有某個(gè)類的 Bean 時(shí),可以在需要自動注入該類 Bean 的地方可以使用@Autowired(required= false)。這等于告訴 Spring:在找不到匹配 Bean 時(shí)也不報(bào)錯。
3.3.1 Resource注解【不常用】
@Resource:可以和@Autowired一樣實(shí)現(xiàn)自動裝配功能
但是跟@Autowired不一樣的是,它默認(rèn)是按照組件名稱進(jìn)行裝配的,按照組件名稱找不到在根據(jù)屬性類型去查找再找不到就報(bào)錯;
他們另一個(gè)不同的地方就是@Autowired是Spring定義的;
@Resource是java規(guī)范。
到此這篇關(guān)于Spring中的自動裝配機(jī)制詳解的文章就介紹到這了,更多相關(guān)Spring自動裝配內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Springboot+poi上傳并處理百萬級數(shù)據(jù)EXCEL
這篇文章主要介紹了使用Springboot+poi上傳并處理百萬級數(shù)據(jù)EXCEL,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12SpringCloud turbine監(jiān)控實(shí)現(xiàn)過程解析
這篇文章主要介紹了SpringCloud turbine監(jiān)控實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12SpringBoot創(chuàng)建WebService方法詳解
這篇文章主要介紹了SpringBoot如何創(chuàng)建WebService,文中有詳細(xì)的實(shí)現(xiàn)步驟以及示例代碼,對學(xué)習(xí)或工作有一定的幫助,需要的朋友跟著小編一起來學(xué)習(xí)吧2023-05-05Java.toCharArray()和charAt()的效率對比分析
這篇文章主要介紹了Java.toCharArray()和charAt()的效率對比分析,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10java循環(huán)練習(xí)的簡單代碼實(shí)例
本篇文章介紹了,java中循環(huán)練習(xí)的一些簡單代碼實(shí)例。需要的朋友參考下2013-04-04