Spring自動注入
spring的ioc
在剛開始學習spring的時候肯定都知道spring的兩個特點:ioc,aop,控制反轉和切面編程,這篇就只說說ioc
ioc是什么:在我們原來的代碼中,如果A依賴了B,那么我們會自己在A類中來new B,創建B的實體來使用,是程式主動的去創建依賴,但是我們在使用spring的了之后還會在A中主動的去創建B嗎?基本不會,因為創建物件的這個操作從原來的我們來控制變成了spring來管理,這個程序就稱為控制反轉,ioc并不是一種技術,而是一種編程思想,代碼的設計思路
那么這樣做的好處是什么?
-
解耦,將物件之間的依賴關系交給spring來處理,避免硬編碼導致高度耦合
-
資源的集中易于管理和配置
而spring實作ioc使用的方法是通過DI(依賴注入),當我們大部分的物件都被spring管理后那么spring也需要將我們A中所依賴的B,C,D...都給填充到A中,這個程序是由spring來管理的,我們只需要按照spring的規則宣告或指定,那么spring也會幫我們完成依賴的注入
自動注入
了解完spring的基本思想后,來回想一下當時學習spring的入門,有沒有說過spring的一個特點就是可以自動注入?
@Component
public class A {
@Autowired
private B b;
}
@Component
public class B {
}
簡單的一段代碼,將A,B交給spring管理,在A中屬性b上添加一個@Autowired,當我們從spring的容器中取出A后發現屬性b居然有值,這個不就是自動注入嗎?
我是認為添加@Autowired注解是不算自動注入的,原因如下
1.名詞解釋
首先,什么叫自動(給翻譯翻譯什么叫tm的自動),生活中肯定都見過自動門,通過過自動門,當我們靠近的時候門會自動打開,通過后門自動關閉,自動就是指我們不需要手動的去開門/關門,那么這里的自動注入也是,我們不需要去手動的在需要注入的屬性上添加一個@Autowired注解
2.官方檔案
https://docs.spring.io/spring-framework/docs/current/reference/html/
在官方檔案的注入方式中,能看到
Dependency injection (DI) is a process whereby objects define their dependencies (that is, the other objects with which they work) only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse (hence the name, Inversion of Control) of the bean itself controlling the instantiation or location of its dependencies on its own by using direct construction of classes or the Service Locator pattern.
Code is cleaner with the DI principle, and decoupling is more effective when objects are provided with their dependencies. The object does not look up its dependencies and does not know the location or class of the dependencies. As a result, your classes become easier to test, particularly when the dependencies are on interfaces or abstract base classes, which allow for stub or mock implementations to be used in unit tests.
DI exists in two major variants: Constructor-based dependency injection and Setter-based dependency injection.
翻譯就是
依賴項注入(DI)是一個程序,物件僅通過建構式引數、工廠方法的引數或物件實體構造或從工廠方法回傳后設定的屬性來定義其依賴項(即與它們一起作業的其他物件),然后,容器在創建bean時注入這些依賴項,這個程序基本上是bean本身的逆程序(因此稱為控制反轉),通過使用類的直接構造或服務定位器模式來控制其依賴項的實體化或位置,
使用DI原則,代碼更干凈,當物件具有依賴關系時,解耦更有效,物件不查找其依賴項,也不知道依賴項的位置或類別,因此,您的類變得更容易測驗,尤其是當依賴項位于介面或抽象基類上時,這允許在單元測驗中使用存根或模擬實作,
DI有兩種主要變體:基于建構式的依賴項注入和基于Setter的依賴項注入,
注意最后一段話:有兩種主要的變體:基于構造和setter方法的依賴注入,也就是說還有其他的注入方式,下面再細說
那么你可能會問?我需要怎么指定或宣告讓他去使用構造或setter方法進行注入而不是我手動指定@Autowired呢?
自動注入模式
還是spring的官網,網頁翻譯的

可以看到spring的自動裝配模式有下面的4種,而默認的是no,也就是不自動注入
現在來說一下setter注入/構造注入和自動注入模式的關系
假設我們需要在大學中找一個人,我們可以問老師/同學,或者去查看學生名單
那么找誰呢?我們可以去問他的名字,手機號,或者根據事件去問
比如我問老師有沒有一個叫張三的學生,可以得到結果,或者我根據手機號去查看學生的名單,也可以得到結果
我也可以去問同學,昨天下午逃課出去上網吧的人是誰?這就是根據事件去問,但是我能根據這個事件去查學生名單找到具體那個人嗎?不能
和spring的一樣,我可以根據型別去注入,通過的是setter方法,根據名稱去注入,也是通過的setter方法
但是當我指定使用構造注入的時候,那么就是通過構造方法進行注入
那么會發現自動注入以及DI的主要實作這里面并沒有所說的 "@Autowired"
那么我們怎么使用自動注入呢?
在xml配置中,只需要將頭定義中加上一句
default-autowire="byType"
<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
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byType">
而在javaconfig中要麻煩一點,我們需要自定義一個配置類,實作BeanFactoryPostProcessor或 BeanDefinitionRegisterPostProcessor來修改beanDefinition的注入模型
@Component
public class BeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
ScannedGenericBeanDefinition a =(ScannedGenericBeanDefinition) beanFactory.getBeanDefinition("a");
a.setAutowireMode(2);
}
}
這個2是啥意思呢,在介面AutowireCapableBeanFactory中,spring定義了每個注入模型的值
public interface AutowireCapableBeanFactory extends BeanFactory {
int AUTOWIRE_NO = 0;
int AUTOWIRE_BY_NAME = 1;
int AUTOWIRE_BY_TYPE = 2;
int AUTOWIRE_CONSTRUCTOR = 3;
@Deprecated
int AUTOWIRE_AUTODETECT = 4;
....
}
那么當我們設定了byName或byType后,只需要提供一個setter方法即可,那么只要這個屬性名稱的bean存在于spring容器中就會被注入
public interface Test {
}
@Component
public class TestA implements Test {
}
@Component
public class A {
@Autowired
private Test testA;
public void setTestA(Test testA) {
System.out.println("走set方法啦");
this.testA = testA;
}
public A() {
System.out.println("走無參構造方法啦");
}
public A(Test testA) {
System.out.println("走有參構造方法啦");
this.testA = testA;
}
@Override
public String toString() {
return "A{" +
"testA=" + testA +
'}';
}
}
結果:
走無參構造方法啦
走set方法啦
A{testA=com.jame.pojo.test.TestA@721e0f4f}
當我設定為byType后結果一樣,就不再粘貼代碼了
而當我設定為根據構造注入后,將注入的模型改成3
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
ScannedGenericBeanDefinition a =(ScannedGenericBeanDefinition) beanFactory.getBeanDefinition("a");
a.setAutowireMode(3);
}
}
結果
走有參構造方法啦
A{testA=com.jame.pojo.test.TestA@28864e92}
那么現在你會明白了所謂的@Autowired并不是自動注入,只要指定了這個bean的注入模型為byType/byName/構造后才是自動注入
@Autowired
@Autowired是使用setter注入是構造注入呢?是使用byName還是byType呢?
回答第一個問題:@Autowired是使用setter注入是構造注入呢?
將我的配置類BeanFactoryPostProcessor上的@Component注解去掉,然后在Test testA添加@Autowired
@Component
public class A {
@Autowired
private Test testA;
//下面的代碼和上面粘貼出的的代碼一樣
}
結果
走無參構造方法啦
A{testA=com.jame.pojo.test.TestA@546a03af}
??什么情況,setter和構造都沒走,因為@Autowired底層使用的反射filed.set()來填充的屬性
DI有兩種主要變體:基于建構式的依賴項注入和基于Setter的依賴項注入
主要的是使用構造和setter,而其他的說的就是這種@Autowired的注入方式
第二個問題:是使用byName還是byType呢?
答案是兩個都是,或者兩個都不是,來看例子
public interface Test {
}
@Component
public class TestA implements Test {
}
@Component
public class A {
@Autowired
private Test test;//注意這里的屬性為test
....省略
}
這樣寫然后從spring容器中獲取肯定能獲取到結果,A中的testA屬性肯定有值的,就不演示了
但是,我給Test介面添加一個實作類TestB
@Component
public class TestB implements Test{
}
繼續執行代碼,發現報錯了
Error creating bean with name 'a': Unsatisfied dependency expressed through field 'test'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.jame.pojo.test.Test' available: expected single matching bean but found 2: testA,testB
創建名稱為“a”的 bean 時出錯:通過欄位“test”表示的依賴關系不滿足;嵌套例外是 org.springframework.beans.factory.NoUniqueBeanDefinitionException:沒有“com.jame.pojo.test.Test”型別的合格 bean 可用:預期單個匹配 bean,但找到 2:testA,testB
到這里是不是就可以說明@Autowired是byType呢?,那怎么證明它也是byName呢?
修改A中的Test test屬性為testA時
@Component
public class A {
@Autowired
private Test testA;//注意這里的屬性為testA
.....省略
}
繼續執行代碼發現又可以了
走無參構造方法啦
A{testA=com.jame.pojo.test.TestA@28864e92}
我們把屬性改為Test testB后繼續測驗
走無參構造方法啦
A{testB=com.jame.pojo.test.TestB@28864e92}
發現也是可以的,那么結論就是:@Autowired既是byType也是byName
當Spring容器中只有一個匹配的類時,會根據Type直接注入,而存在多個匹配的時候(接收的屬性定義為介面,有多個實作類),會直接拋出例外
這時候我們可以使用@Qualifier("testA")來指定具體哪一個類來注入
或者修改屬性的名字為需要注入類的名稱(首字母小寫)
測驗了一下@Qualifier()是高于byName的,也就是說即使存在兩個匹配的類,即使屬性名叫testB,我只要使用@Qualifier("testA")來指定bean,那么注入的就是testA
蕪湖沒了,拜拜
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/469706.html
標籤:其他
