文章目錄
-
-
-
-
-
- IOC控制反轉 Inversion Of Control
- IOC原理
- IOC的好處
- DI Dependency Injection 依賴注入
- DI的三種實作方式
- IOC自動化裝配
-
-
-
-
IOC控制反轉 Inversion Of Control
所謂IOD對于spring架構來說,就是由spring來負責控制對象的生命周期和對詳見的關系。就比如找對象,原本我們需要了解對象的一些資訊,然後new一個(或者從JNDI中查詢一個),使用完之後還得将對象銷毀,這裡對象始終和其他類或接口耦合起來。
那麼IOC就想一個婚介中心,管理了很多對象,我們可以向婚介中心提出想要怎麼樣的對象,如果婚介中心給我們的人選不符合要求,那麼我們就會抛出異常。整個過程不再由我們自己控制。
就是所有的類都會在spring中登記,告訴spring你是個什麼東西,需要什麼東西,然後spring就會在系統運作到适當的時候,把你需要的東西給你,同時也把你交給其他需要你的東西。所有的類的建立、銷毀都由spring控制,也就是說控制對象生命周期的不再是引用它的對象,而是spring。對于某個具體的對象而言,以前是它控制其他對象,現在是所有對象都被spring控制,這個就叫控制反轉
IOC原理
其實就是反射+工廠模式
我們可以把IOC容器的工作模式看做是工廠模式的升華,可以把IOC容器看作是一個工廠,這個工廠裡要生産的對象都在配置檔案中給出定義,然後利用程式設計語言提供的反射機制,根據配置檔案中給出的類名生成相應的對象。從實作來看,IOC是把以前在工廠方法裡寫死的對象生成代碼,改變為由配置檔案來定義,也就是把工廠和對象生成這兩者獨立分隔開來,目的就是提高靈活性和可維護性。
IOC是目的,DI是一種手段
IOC的好處
ioc思想最核心的就是,資源不由使用資源的雙方管理,而由不使用資源的第三方管理
- 資源集中管理,實作資源的可配置和易管理
- 降低了使用資源雙方的依賴程度,也就是耦合度
- 代碼更容易複用
- 開發更友善組織分工
-
軟體演化有更好的靈活性,能快速響應需求變化,維護代價更小
竊以為你隻需砥砺前行,專心做事,經磨煉鋒寶劍,曆苦寒香梅花,對象這種東西。。等配置設定吧orz
DI Dependency Injection 依賴注入
在系統運作過程中,動态的向某個對象注入它所依賴的對象。接上面的說法,婚介中心會在适當的時候給你一個對象,進入到你的生命中,這就是依賴注入。
DI的三種實作方式
- 設定注入—Setter Injection
- 構造方法注入—Constructor Injection
- 接口注入—Interface Injection
public class Person{
private Dog dog;
private Ingteger age;
private String name;
//setter
//getter
}
<bean id="person" class="com.myspring.Person">
<property name="age" value="18"/>
<property name="name" value="胡八萬"/>
<property name="dog" ref="dog"/>
</bean>
<bean id="dog" class="com.myspring.Dog">
<property name="name" value="二哈"/>
</bean>
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=ctx.getBean("person",Person.class);
這種方法要求屬性必須實作setter
ref=""表示對象的引用
ref用來指向其他的bean
空值标簽
另外注入集合
< list >:注入一列值,允許重複
< set >:注入一列值,不允許重複
< map >:注入鍵(名)值對的集合,名稱和值可以是任何類型
< props >:注入鍵(名)值對的集合,名稱和值可以是任何類
private List list;
private Map map;
private Properties prop;
<bean id="mjc" class="com.myspring.JavaCollection">
<property name="list">
<!-- 注入集合 值可重複 set就不舉例了-->
<list>
<value>list1</value>
<value>list1</value>
</list>
</property>
<property name="map">
<!-- 注入map -->
<map>
<entry key="小紅">
<value>18</value>
</entry>
</map>
</property>
<property name="prop">
<!-- 注入properties -->
<props>
<prop key="小李">男</prop>
</props>
</property>
</bean>
還有一種有趣的方式spring表達式,類似于EL表達式,可以讀取一個bean對象或者集合中的内容:#{bean.屬性}
<bean id="personClone" class="com.myspring.Person">
<property name="age" value="#{person.age}"/>
<property name="name" value="#{person.name}"/>
</bean>
public Person(Integer age,String name,Dog dog){
this.age = age;
//...
}
<bean id="person" class="com.oak.entity.Person">
<constructor-arg index="1" value="八萬"/>
<constructor-arg index="2" value="18"/>
<constructor-arg index="3" ref="dog"/>
</bean>
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=ctx.getBean("person",Person.class);
構造方法中的參數必須與配置檔案中的參數個數一直否則報錯
由于java中不會維護形參的名稱,是以使用name屬性進行注入時可能會有風險
public Person(Integer age,String args0,Dog dog){}
Interface Injection
它是在一個接口中定義需要注入的資訊,并通過接口完成注入。Apache Avalon是一個較為典型的接口注入形IOC容器
以使用者注冊為例,首先定義一個接口,它的用途是将一個傳回一個Person執行個體
public interface PersonInterfaceInject {
public Person getPerson();
}
同時,我們需要配置一個xml
<!-- Person的一個執行個體bean,定義每次傳回不同的執行個體對象 -->
<bean id="liangzai" class="com.myspring.Person" p:name="胡八萬"
p:age="18" scope="prototype" />
<!-- 實施方法注入 -->
<bean id="personInterfaceInject" class="com.myspring.PersonInterfaceInject">
<lookup-method name="getPerson" bean="liangzai" />
</bean>
最後測試一下
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
PersonInterfaceInject personInterfaceInject=applicationContext.getBean("personInterfaceInject",Person.class);
Person person1=personInterfaceInject.getPerson();
Person Person2=personInterfaceInject.getPerson();
System.out.println(person1);
System.out.println(person2);
System.out.println("person2==person1?:"+(person2==person1));
name:胡八萬 age:18
name:胡八萬 age:18
person2==person1?:false
最後輸出可以看到person1和person2是不同執行個體
了解:“Lookup方法”可以使Spring替換一個bean原有的,擷取其它對象具體的方法,并自動傳回在容器中的查找結果。
IOC自動化裝配
set注入和構造注入有時在做配置時比較麻煩。是以架構為了提高開發效率,提供自動裝配功能,簡化配置。Spring架構式預設不支援自動裝配的,要想使用自動裝配需要修改spring配置檔案中标簽的autowire屬性。自動裝配屬性有5個值可選,分别代表不同的含義。
-
byName
從Spring環境中擷取目标對象時,目标對象中的屬性會根據名稱在整個Spring環境中查找标簽的id屬性值。如果有相同的,那麼擷取這個對象,實作關聯。
整個Spring環境:表示所有的spring配置檔案中查找,那麼id不能有重複的。
-
byType
從Spring環境中擷取目标對象時,目标對象中的屬性會根據類型在整個spring環境中查找标簽的class屬性值。如果有相同的,那麼擷取這個對象,實作關聯。
缺點:如果存在多個相同類型的bean對象,會出錯。
如果屬性為單一類型的資料,那麼查找到多個關聯對象會發生錯誤。
如果屬性為數組或集合(泛型)類型,那麼查找到多個關聯對象不會發生異常。
-
constructor(3以上已不能用)
使用構造方法完成對象注入,其實也是根據構造方法的參數類型進行對象查找,相當于采用byType的方式
如果沒找到則抛出異常
-
autodetect
自動選擇:如果對象沒有無參數的構造方法,那麼自動選擇constructor的自動裝配方式進行構造注入。如果對象含有無參數的構造方法,那麼自動選擇byType的自動裝配方式進行setter注入。
-
no
預設情況下不自動裝配
學生類
public class Student {
private String ID;
private String name;
private int age;
private String sex;
//setter getter
}
教師類
public class Teacher {
private String name;
private int age;
private String sex;
//setter getter
}
教學檔案類
public class TeachFile {
private Teacher teacher;
private Student student;
public TeachFile() {
}
public TeachFile(Teacher teacher, Student student) {
this.teacher = teacher;
this.student = student;
}
public void print() {
System.out.println("------教師資訊------");
System.out.println("姓名:" + teacher.getName());
System.out.println("年齡:" + teacher.getAge());
System.out.println("性别:" + teacher.getSex());
System.out.println();
System.out.println("------學生資訊------");
System.out.println("學号:" + student.getID());
System.out.println("姓名:" + student.getName());
System.out.println("年齡:" + student.getAge());
System.out.println("性别:" + student.getSex());
}
//setter getter
}
XML中對TeachFile自動裝配
<bean id="student" class="com.autobean.Student">
<property name="ID" value="80" />
<property name="name" value="阿王" />
<property name="age" value="23" />
<property name="sex" value="男" />
</bean>
<bean id="teacher" class="com.autobean.Teacher">
<property name="name" value="何老師" />
<property name="age" value="43" />
<property name="sex" value="女" />
</bean>
<!-- 預設情況下,通過'ref’來裝配bean -->
<bean id="teachFile1" class="com.autobean.TeachFile" >
<property name="teacher" ref="teacher" />
<property name="student" ref="student" />
</bean>
<!--根據byName自動裝配bean -->
<bean id="teachFile2" autowire="byName" class="com.autobean.TeachFile" />
<!--根據byType自動裝配bean -->
<bean id="teachFile3" autowire="byType" class="com.autobean.TeachFile" />
<!--根據constructor自動裝配bean -->
<bean id="teachFile4" autowire="constructor" class="com.autobean.TeachFile"/>
測試
Resource res = new ClassPathResource("applicationContext.xml");
BeanFactory bf = new XmlBeanFactory(res);
TeachFile tf1 = (TeachFile) bf.getBean("teachFile1");
TeachFile tf2 = (TeachFile) bf.getBean("teachFile2");
TeachFile tf3 = (TeachFile) bf.getBean("teachFile3");
TeachFile tf4 = (TeachFile) bf.getBean("teachFile4");
System.out.println("預設情況下,通過'ref’來裝配bean");
tf1.print();
System.out.println("根據byName自動裝配bean");
tf2.print();
System.out.println("根據byType自動裝配bean");
tf3.print();
System.out.println("根據constructor自動裝配bean");
tf4.print();
輸出:
預設情況下,通過'ref’來裝配bean
------教師資訊------
姓名:何老師
年齡:43
性别:女
------學生資訊------
學号:80
姓名:阿王
年齡:23
性别:男
根據byName自動裝配bean
------教師資訊------
姓名:何老師
年齡:43
性别:女
------學生資訊------
學号:80
姓名:阿王
年齡:23
性别:男
根據byType自動裝配bean
------教師資訊------
姓名:何老師
年齡:43
性别:女
------學生資訊------
學号:80
姓名:阿王
年齡:23
性别:男
根據constructor自動裝配bean
------教師資訊------
姓名:何老師
年齡:43
性别:女
------學生資訊------
學号:80
姓名:阿王
年齡:23
性别:男