Spring的DI依賴注入詳解
1、什么是DI依賴注入?
spring動態(tài)的向某個對象提供它所需要的其他對象。這一點是通過DI(Dependency Injection,依賴注入)來實現(xiàn)的。比如對象A需要操作數(shù)據(jù)庫,以前我們總是要在A中自己編寫代碼來獲得一個Connection對象,有了 spring我們就只需要告訴spring,A中需要一個Connection,至于這個Connection怎么構(gòu)造,何時構(gòu)造,A不需要知道。在系統(tǒng)運行時,spring會在適當(dāng)?shù)臅r候制造一個Connection,然后像打針一樣,注射到A當(dāng)中,這樣就完成了對各個對象之間關(guān)系的控制。A需要依賴 Connection才能正常運行,而這個Connection是由spring注入到A中的,依賴注入的名字就這么來的。那么DI是如何實現(xiàn)的呢? Java 1.3之后一個重要特征是反射(reflection),它允許程序在運行的時候動態(tài)的生成對象、執(zhí)行對象的方法、改變對象的屬性,spring就是通過反射來實現(xiàn)注入的。
簡單來說什么是依賴注入,就是給屬性賦值(包括基本數(shù)據(jù)類型和引用數(shù)據(jù)類型)
2、利用 set 方法給屬性賦值
第一步:創(chuàng)建工程,并導(dǎo)入相應(yīng)的 jar 包
第二步:創(chuàng)建實體類 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 類,基本數(shù)據(jù)類以及集合數(shù)據(jù)類型。
第三步:在 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>
第四步:測試
//利用 set 方法給對象賦值 @Test public void testSet(){ //1、啟動 spring 容器 //2、從 spring 容器中取出數(shù)據(jù) //3、通過對象調(diào)用方法 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person) context.getBean("person"); System.out.println(person.getPname());//vae }
3、利用 構(gòu)造函數(shù) 給屬性賦值
第一步:在實體類 Per'son.java 中添加兩個構(gòu)造方法:有參和無參
//默認構(gòu)造函數(shù) public Person(){} //帶參構(gòu)造函數(shù) public Person(Long pid,Student students){ this.pid = pid; this.students = students; }
第二步:在 applicationContext.xml 中進行賦值
<!-- 根據(jù)構(gòu)造函數(shù)賦值 --> <!-- index 代表參數(shù)的位置 從0開始計算 type 指的是參數(shù)的類型,在有多個構(gòu)造函數(shù)時,可以用type來區(qū)分,要是能確定是那個構(gòu)造函數(shù),可以不用寫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>
第三步:測試
//利用 構(gòu)造函數(shù) 給對象賦值 @Test public void testConstrutor(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person) context.getBean("person_con"); System.out.println(person.getPid());//1 }
總結(jié):
1、如果spring的配置文件中的bean中沒有<constructor-arg>該元素,則調(diào)用默認的構(gòu)造函數(shù)
2、如果spring的配置文件中的bean中有<constructor-arg>該元素,則該元素確定唯一的構(gòu)造函數(shù)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
半小時實現(xiàn)Java手擼網(wǎng)絡(luò)爬蟲框架(附完整源碼)
最近在做一個搜索相關(guān)的項目,需要爬取網(wǎng)絡(luò)上的一些鏈接存儲到索引庫中,自己寫了一個簡單的網(wǎng)絡(luò)爬蟲,感興趣的可以了解一下2021-06-06MyBatis使用annonation定義類型映射的簡易用法示例
這篇文章主要介紹了MyBatis使用annonation定義類型映射的簡易用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09@Bean注解和@Configuration、@Component注解組合使用的區(qū)別
這篇文章主要介紹了@Bean注解和@Configuration、@Component注解組合使用的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11如何使用IDEA創(chuàng)建MAPPER模板過程圖解
這篇文章主要介紹了如何使用IDEA創(chuàng)建MAPPER模板,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05Mybatis如何使用@Mapper和@MapperScan注解實現(xiàn)映射關(guān)系
這篇文章主要介紹了Mybatis使用@Mapper和@MapperScan注解實現(xiàn)映射關(guān)系,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10