Spring中@Autowired注解作用在方法上和屬性上說明
@Autowired注解作用在方法上
(1)該方法如果有參數(shù),會(huì)使用autowired的方式在容器中查找是否有該參數(shù)
(2)會(huì)執(zhí)行該方法
所以如果把@Autowired放在setter方法上,就等于給對(duì)象的屬性賦值
@Autowired注解作用在屬性上
即為在容器中創(chuàng)建對(duì)象時(shí)給該項(xiàng)屬性注入值,效果同@Autowired在setter方法。
@Autowired注解的使用和注入規(guī)則
作為一個(gè)Spring開發(fā)者對(duì)@Autowired注解必定是非常了解了, 顧名思義自動(dòng)裝配,應(yīng)該是Spring會(huì)自動(dòng)將我們標(biāo)記為@Autowired的元素裝配好,與其猜測不如看看它的定義:
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Autowired { ? ? boolean required() default true; ? }
很明顯這個(gè)注解可以用到構(gòu)造器,變量域,方法,注解類型和方法參數(shù)上。文檔上這樣描述:將一個(gè)構(gòu)造器,變量域,setter方法,config方法標(biāo)記為被Spring DI 工具自動(dòng)裝配。換句話說,在Spring創(chuàng)建bean的過程中,會(huì)為這個(gè)bean中標(biāo)有@Autowired注解的構(gòu)造器,變量域,方法和方法參數(shù)中自動(dòng)注入我們需要的已經(jīng)在Spring IOC容器里面的bean,,而無需我們手動(dòng)完成,并且注入的bean都是單實(shí)例,也就是在兩個(gè)bean中都依賴第三個(gè)bean,那么這兩個(gè)bean中注入的第三個(gè)bean會(huì)是同一個(gè)bean(JVM中指向的地址相同)。
在@Autowired注解里面有一個(gè)required屬性,該屬性默認(rèn)為true,當(dāng)為true時(shí),表示去Spring IOC中查找相應(yīng)的bean,如果找不到,則會(huì)報(bào)錯(cuò),如果為false時(shí),表示去Spring IOC中查找相應(yīng)的bean,如果找不到,則直接忽略,不再進(jìn)行注入。
@Autowired注解的注入規(guī)則:默認(rèn)按照類型進(jìn)行注入,如果IOC容器中存在兩個(gè)及以上的相同類型的bean時(shí),根據(jù)bean的名稱進(jìn)行注入,如果沒有指定名稱的bean,則會(huì)報(bào)錯(cuò)。
可以使用@Qualifier("wheel")來使用指定id的bean,也可以在注入bean時(shí),添加@Primary注解,優(yōu)先添加一個(gè)bean,其規(guī)則如下:
如果指定添加了@Qualifier("wheel")則按照指定的bean id進(jìn)行添加(優(yōu)先級(jí)最高),找不到則直接報(bào)錯(cuò)。如果沒有添加@Qualifier而添加了@Primary注解,則首先添加標(biāo)注了@Primary注解的bean。當(dāng)即存在@Qualifier注解也存在@Primary注解注解,則按照@Qualifier指定的bean id注入,找不到直接報(bào)錯(cuò)。
很多人java開發(fā)者都知道@Autowired注解,但是真正用的好的也不多(反正系統(tǒng)的學(xué)習(xí)Spring之前我是不知道的),那下面讓我們來看一下@Autowired的用法:
1.使用在變量域上面
這個(gè)相信大家都已經(jīng)清楚了,Spring會(huì)幫我們注入我們想要的bean,看下面的例子:
package it.cast.circularDependency; ? @Component public class Wheel { ? } ? @Component public class Car { ? ? ? @Autowired ? ? private Wheel wheel2; ? ? ? public Wheel getWheel() { ? ? ? ? return wheel2; ? ? } ? ? ? public void setWheel(Wheel wheel2) { ? ? ? ? this.wheel2 = wheel2; ? ? } } ? @ComponentScan({"it.cast.circularDependency"}) public class AutowiredConfig { ? }
下面進(jìn)行測試,打印的結(jié)果顯示可以拿到Wheel類,說明@Autowired注解在IOC容器中只有一個(gè)類型的bean時(shí),按照類型進(jìn)行注入。
? ? @Test ? ? public void AutowiredConfigTest(){ ? ? ? ? AnnotationConfigApplicationContext context = ? ? ? ? ? ? ? ? new AnnotationConfigApplicationContext(AutowiredConfig.class); ? ? ? ? ? Car bean = context.getBean(Car.class); ? ? ? ? System.out.println(bean.getWheel()); ? ? } ? //打印結(jié)果: // ? it.cast.circularDependency.Wheel@3eb25e1a
下面看一下當(dāng)IOC容器中有兩個(gè)Wheel類型的bean時(shí)的情況,改造Wheel類,增加一個(gè)屬性標(biāo)識(shí)用于記錄向Car類中注入的哪個(gè)Wheel的bean,在AutowiredConfig配置類中添加一個(gè)bean,bean的名稱默認(rèn)為方法名,也就是wheel1。
@Component public class Wheel { ? ? private int num = 2; ? //通過包掃描的方式注入的bean的num值為2 ? ? ? public int getNum() { ? ? ? ? return num; ? ? } ? ? ? public void setNum(int num) { ? ? ? ? this.num = num; ? ? } } ? @Component public class Car { ? ? ? @Autowired ? ? private Wheel wheel3;//將變量名改成wheel3,IOC容器中Wheel類型的bean的名稱只有wheel和wheel1 ? ? ? public Wheel getWheel() { ? ? ? ? return wheel3; ? ? } ? ? ? public void setWheel(Wheel wheel3) { ? ? ? ? this.wheel3 = wheel3; ? ? } } ? ? @Configuration @ComponentScan({"it.cast.circularDependency"}) public class AutowiredConfig { ? ? ? @Bean ? ? public Wheel wheel1(){? ? ? ? ? Wheel wheel = new Wheel();//通過配置類注入bean的方式num值為0 ? ? ? ? wheel.setNum(0); ? ? ? ? return wheel; ? ? } }
這時(shí)在Spring IOC中有兩個(gè)Wheel類型的bean了,Car在注入Wheel類型的bean時(shí),會(huì)根據(jù)變量名wheel3去找,也就是說會(huì)去找類型為Wheel,名稱為wheel3的bean,顯然是找不到的,也就會(huì)報(bào)錯(cuò)。
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'car':
Unsatisfied dependency expressed through field 'wheel3';
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'it.cast.circularDependency.Wheel' available:
expected single matching bean but found 2: wheel,wheel1
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'car': Unsatisfied dependency expressed through field 'wheel3';
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'it.cast.circularDependency.Wheel' available:
expected single matching bean but found 2: wheel,wheel1
上面為報(bào)錯(cuò)的日志打印,大致意思說的在創(chuàng)建名稱為car的bean時(shí),不能為變量域wheel3完成屬性注入,因?yàn)檎业搅藘蓚€(gè)bean,分別是wheel和wheel1。
如果我們把Car中的wheel3換成wheel就可以完成注入了,而且注入的bean是通過包掃描注入IOC的bean:
@Component public class Wheel { ? ? private int num = 2; ? //通過包掃描的方式注入的bean的num值為2 ? ? ? public int getNum() { ? ? ? ? return num; ? ? } ? ? ? public void setNum(int num) { ? ? ? ? this.num = num; ? ? } } ? @Component public class Car { ? ? ? @Autowired ? ? private Wheel wheel;//將變量名改成wheel1,IOC容器中Wheel類型的bean的名稱只有wheel和wheel1 ? ? ? public Wheel getWheel() { ? ? ? ? return wheel; ? ? } ? ? ? public void setWheel(Wheel wheel3) { ? ? ? ? this.wheel = wheel; ? ? } } ? ? @Configuration @ComponentScan({"it.cast.circularDependency"}) public class AutowiredConfig { ? ? ? @Bean ? ? public Wheel wheel1(){? ? ? ? ? Wheel wheel = new Wheel();//通過配置類注入bean的方式num值為0 ? ? ? ? wheel.setNum(0); ? ? ? ? return wheel; ? ? } }
在測試類中打印num值看看注入的是哪個(gè)bean:
? ? @Test ? ? public void AutowiredConfigTest(){ ? ? ? ? AnnotationConfigApplicationContext context = ? ? ? ? ? ? ? ? new AnnotationConfigApplicationContext(AutowiredConfig.class); ? ? ? ? ? Car bean = context.getBean(Car.class); ? ? ? ? System.out.println(bean.getWheel().getNum()); ? ? } ? ? ?//打印結(jié)果: ? ?// ? ?2
那么就驗(yàn)證了上面所說的注入規(guī)則:默認(rèn)按照類型進(jìn)行注入,如果IOC容器中存在兩個(gè)及以上的相同類型的bean時(shí),根據(jù)bean的名稱進(jìn)行注入,如果沒有指定名稱的bean,則會(huì)報(bào)錯(cuò)。
@Autowired注解使用在變量域中還可以解決循環(huán)依賴的問題,循環(huán)依賴問題就是A對(duì)象中注入了B對(duì)象,B對(duì)象中注入了A對(duì)象,循環(huán)依賴在面試Spring這一塊的知識(shí)應(yīng)該經(jīng)常會(huì)被問題,關(guān)于循環(huán)依賴的問題,在后面的博客中會(huì)更新。
2.@Autowired注解使用在構(gòu)造器上面
@Autowired使用在構(gòu)造器上面有幾條需要特別注意的點(diǎn):
1.@Autowired標(biāo)注在構(gòu)造器上面不能解決循環(huán)依賴構(gòu)造的問題
2.@Autowired可以標(biāo)注在同一個(gè)類的多個(gè)構(gòu)造器上面,但是required屬性必須都為false,當(dāng)required有一個(gè)為true時(shí),不允許其他構(gòu)造器標(biāo)有@Autowired注解,即使required屬性為false也不行。
@Component public class A { ? ? private B b; ? ? private C c; ? ? ? @Autowired ? ? public A(B b, C c) { ? ? ? ? System.out.println("b=" + b + ", c=" + c); ? ? ? ? this.b = b; ? ? ? ? this.c = c; ? ? } } ? @Component public class B { ? } ? @Component public class C { } ? //打印結(jié)果: // ?b=it.cast.circularDependency.B@68e965f5, c=it.cast.circularDependency.C@6f27a732
@Autowired標(biāo)注在構(gòu)造器上面,在B創(chuàng)建的過程中,會(huì)去Spring IOC中拿到需要的注入的bean,完成B的創(chuàng)建,其實(shí)在只有一個(gè)構(gòu)造器的情況中,@Autowired可以不加,因?yàn)镾pring內(nèi)部有自動(dòng)推斷構(gòu)造器的能力,這個(gè)如果想了解自動(dòng)推斷構(gòu)造器的同學(xué)可以自行百度(實(shí)在是太難了,一臉蒙蔽)。
下面看一個(gè)構(gòu)造器循壞依賴的案例:
@Component public class C { ? ? private B b; ? ? ? public C(B b) { ? ? ? ? this.b = b; ? ? } } ? @Component public class B { ? ? private C c; ? ? ? public B(C c) { ? ? ? ? this.c = c; ? ? } }
Spring目前不能解決構(gòu)造器的循環(huán)依賴,所以在項(xiàng)目中使用的時(shí)候要格外注意一下,錯(cuò)誤日志:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'a' defined in file
[E:\IdeaProjects\javaBasis\spring\target\classes\it\cast\circularDependency\A.class]:
Unsatisfied dependency expressed through constructor parameter 0;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'b' defined in file
[E:\IdeaProjects\javaBasis\spring\target\classes\it\cast\circularDependency\B.class]:
Unsatisfied dependency expressed through constructor parameter 0;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'c' defined in file
[E:\IdeaProjects\javaBasis\spring\target\classes\it\cast\circularDependency\C.class]:
Unsatisfied dependency expressed through constructor parameter 0;
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'b':
Requested bean is currently in creation: Is there an unresolvable circular reference?
下面看一下關(guān)于多個(gè)@Autowired標(biāo)注的構(gòu)造器的案例:
@Component public class A { ? ? private B b; ? ? private C c; ? ? ? @Autowired(required = false) ? ? public A(B b) { ? ? ? ? this.b = b; ? ? } ? ? ? @Autowired ? ? public A(B b, C c) { ? ? ? ? System.out.println("b=" + b + ", c=" + c); ? ? ? ? this.b = b; ? ? ? ? this.c = c; ? ? } } ? @Component public class B { ? } ? @Component public class C { ? }
上面已經(jīng)說到,如果@Autowired注解的屬性required為true時(shí),不允許再出現(xiàn)其他構(gòu)造器上面標(biāo)有@Autowired注解(@Autowired注解的required默認(rèn)為true,所以上面的會(huì)報(bào)錯(cuò))
錯(cuò)誤日志為:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'a': Invalid autowire-marked constructors:
[public it.cast.circularDependency.A(it.cast.circularDependency.B)].
Found constructor with 'required' Autowired annotation:
public it.cast.circularDependency.A(it.cast.circularDependency.B,it.cast.circularDependency.C)
使用下面的寫法就不會(huì)出現(xiàn)錯(cuò)誤了,Spring支持多個(gè)構(gòu)造器有@Autowired注解,但是required屬性必須都是false
@Component public class A { ? ? private B b; ? ? private C c; ? ? ? @Autowired(required = false)? ? ? public A(B b) { ? ? ? ? this.b = b; ? ? } ? ? ? @Autowired(required = false) ? ? public A(B b, C c) { ? ? ? ? System.out.println("b=" + b + ", c=" + c); ? ? ? ? this.b = b; ? ? ? ? this.c = c; ? ? } } ? @Component public class B { ? } ? @Component public class C { ? }
關(guān)于@Autowired標(biāo)注在方法上就不多介紹,會(huì)首先拿到方法的參數(shù)列表,然后根據(jù)上面所說的注入規(guī)則去Spring IOC中找相應(yīng)的bean。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)批量導(dǎo)入.csv文件到mysql數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)批量導(dǎo)入.csv文件到mysql數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08java拋出異常后,后續(xù)代碼是否繼續(xù)執(zhí)行詳解
這篇文章主要給大家介紹了關(guān)于java拋出異常后,后續(xù)代碼是否繼續(xù)執(zhí)行詳?shù)南嚓P(guān)資料,在Java編程中,異常是當(dāng)程序執(zhí)行時(shí)遇到問題時(shí)拋出的一種特殊情況,需要的朋友可以參考下2023-07-07Spring?Aop+Redis實(shí)現(xiàn)優(yōu)雅記錄接口調(diào)用情況
通常情況下,開發(fā)完一個(gè)接口,無論是在測試階段還是生產(chǎn)上線,我們都需要對(duì)接口的執(zhí)行情況做一個(gè)監(jiān)控,所以本文為大家整理了Spring統(tǒng)計(jì)接口調(diào)用的多種方法,希望對(duì)大家有所幫助2023-06-06使用Spring Boot搭建Java web項(xiàng)目及開發(fā)過程圖文詳解
這篇文章主要介紹了使用Spring Boot搭建Java web項(xiàng)目及開發(fā)過程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06了解Maven的<relativePath/>標(biāo)簽用法
這篇文章主要介紹了了解Maven的<relativePath/>標(biāo)簽用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04java實(shí)現(xiàn)輸出任意整數(shù)的每一位
這篇文章主要介紹了java實(shí)現(xiàn)輸出任意整數(shù)的每一位,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01解決IDEA報(bào)錯(cuò)war?exploded?is?not?valid問題
在使用IntelliJ?IDEA時(shí)遇到'[projectname]warexploded'無效的問題,可以通過清除項(xiàng)目列表、重新導(dǎo)入項(xiàng)目和配置新的Tomcat來解決,確保在Tomcat配置中,將ApplicationContext修改為僅包含一個(gè)'/',這一方法或許能幫助遇到相似問題的開發(fā)者2024-09-09