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

