上一篇博客咱們主要講解了IOC控制反轉,也就是說IOC 讓程序員不在關注怎麼去建立對象,而是關注與對象建立以後的操做,把對象的建立、初始化、銷燬等工做交給spring容器來作。那麼建立對象的時候,有可能依賴於其餘的對象,即類的屬性如何賦值?這也是咱們這篇博客講解 Spring 另外一個核心要點:DI依賴注入。java
PS:本篇博客源碼連接:百度雲盤/Java實例/java框架—spring/Spring_02_DI.zip程序員
spring動態的向某個對象提供它所須要的其餘對象。這一點是經過DI(Dependency Injection,依賴注入)來實現的。好比對象A須要操做數據庫,之前咱們老是要在A中本身編寫代碼來得到一個Connection對象,有了 spring咱們就只須要告訴spring,A中須要一個Connection,至於這個Connection怎麼構造,什麼時候構造,A不須要知道。在系統運行時,spring會在適當的時候製造一個Connection,而後像打針同樣,注射到A當中,這樣就完成了對各個對象之間關係的控制。A須要依賴 Connection才能正常運行,而這個Connection是由spring注入到A中的,依賴注入的名字就這麼來的。那麼DI是如何實現的呢? Java 1.3以後一個重要特徵是反射(reflection),它容許程序在運行的時候動態的生成對象、執行對象的方法、改變對象的屬性,spring就是經過反射來實現注入的。spring
簡單來講什麼是依賴注入,就是給屬性賦值(包括基本數據類型和引用數據類型)數據庫
第一步:建立工程,並導入相應的 jar 包app
第二步:建立實體類 Person框架
package com.ys.di; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Person { private Long pid; private String pname; private Student students; private List lists; private Set sets; private Map maps; private Properties properties; public Long getPid() { return pid; } public void setPid(Long pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Student getStudents() { return students; } public void setStudents(Student students) { this.students = students; } public List getLists() { return lists; } public void setLists(List lists) { this.lists = lists; } public Set getSets() { return sets; } public void setSets(Set sets) { this.sets = sets; } public Map getMaps() { return maps; } public void setMaps(Map maps) { this.maps = maps; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } }
咱們看到這個實體類包括引用類型 Student 類,基本數據類以及集合數據類型。函數
第三步:在 applicationContext.xml 中進行賦值測試
<!-- property是用來描述一個類的屬性 基本類型的封裝類、String等須要值的類型用value賦值 引用類型用ref賦值 --> <bean id="person" class="com.ys.di.Person"> <property name="pid" value="1"></property> <property name="pname" value="vae"></property> <property name="students"> <ref bean="student"/> </property> <property name="lists"> <list> <value>1</value> <ref bean="student"/> <value>vae</value> </list> </property> <property name="sets"> <set> <value>1</value> <ref bean="student"/> <value>vae</value> </set> </property> <property name="maps"> <map> <entry key="m1" value="1"></entry> <entry key="m2" > <ref bean="student"/> </entry> </map> </property> <property name="properties"> <props> <prop key="p1">p1</prop> <prop key="p2">p2</prop> </props> </property> </bean> <bean id="student" class="com.ys.di.Student"></bean>
第四步:測試this
//利用 set 方法給對象賦值 @Test public void testSet(){ //一、啓動 spring 容器 //二、從 spring 容器中取出數據 //三、經過對象調用方法 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person) context.getBean("person"); System.out.println(person.getPname());//vae }
第一步:在實體類 Per'son.java 中添加兩個構造方法:有參和無參
//默認構造函數 public Person(){} //帶參構造函數 public Person(Long pid,Student students){ this.pid = pid; this.students = students; }
第二步:在 applicationContext.xml 中進行賦值
<!-- 根據構造函數賦值 --> <!-- index 表明參數的位置 從0開始計算 type 指的是參數的類型,在有多個構造函數時,能夠用type來區分,要是能肯定是那個構造函數,能夠不用寫type value 給基本類型賦值 ref 給引用類型賦值 --> <bean id="person_con" class="com.ys.di.Person"> <constructor-arg index="0" type="java.lang.Long" value="1"> </constructor-arg> <constructor-arg index="1" type="com.ys.di.Student" ref="student_con"></constructor-arg> </bean> <bean id="student_con" class="com.ys.di.Student"></bean>
第三步:測試
//利用 構造函數 給對象賦值 @Test public void testConstrutor(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person) context.getBean("person_con"); System.out.println(person.getPid());//1 }
總結:
一、若是spring的配置文件中的bean中沒有<constructor-arg>該元素,則調用默認的構造函數
二、若是spring的配置文件中的bean中有<constructor-arg>該元素,則該元素肯定惟一的構造函數