Spring的DI依賴注入詳解
1、什么是DI依賴注入?
spring動態(tài)的向某個對象提供它所需要的其他對象。這一點(diǎn)是通過DI(Dependency Injection,依賴注入)來實(shí)現(xiàn)的。比如對象A需要操作數(shù)據(jù)庫,以前我們總是要在A中自己編寫代碼來獲得一個Connection對象,有了 spring我們就只需要告訴spring,A中需要一個Connection,至于這個Connection怎么構(gòu)造,何時構(gòu)造,A不需要知道。在系統(tǒng)運(yùn)行時,spring會在適當(dāng)?shù)臅r候制造一個Connection,然后像打針一樣,注射到A當(dāng)中,這樣就完成了對各個對象之間關(guān)系的控制。A需要依賴 Connection才能正常運(yùn)行,而這個Connection是由spring注入到A中的,依賴注入的名字就這么來的。那么DI是如何實(shí)現(xiàn)的呢? Java 1.3之后一個重要特征是反射(reflection),它允許程序在運(yùn)行的時候動態(tài)的生成對象、執(zhí)行對象的方法、改變對象的屬性,spring就是通過反射來實(shí)現(xiàn)注入的。
簡單來說什么是依賴注入,就是給屬性賦值(包括基本數(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;
}
}我們看到這個實(shí)體類包括引用類型 Student 類,基本數(shù)據(jù)類以及集合數(shù)據(jù)類型。
第三步:在 applicationContext.xml 中進(jìn)行賦值
<!--
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ù) 給屬性賦值
第一步:在實(shí)體類 Per'son.java 中添加兩個構(gòu)造方法:有參和無參
//默認(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開始計算
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)用默認(rèn)的構(gòu)造函數(shù)
2、如果spring的配置文件中的bean中有<constructor-arg>該元素,則該元素確定唯一的構(gòu)造函數(shù)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
SpringBoot如何通過@Profile注解配置多環(huán)境
在Spring中,可以使用配置文件的方式來指定不同環(huán)境下所需要的配置信息,本文給大家介紹SpringBoot如何通過@Profile注解配置多環(huán)境,感興趣的朋友跟隨小編一起看看吧2023-06-06
SpringBoot開發(fā)之?dāng)r截器實(shí)例
這篇文章主要介紹了SpringBoot開發(fā)之?dāng)r截器實(shí)例,Spring?Boot簡介Spring?Boot發(fā)展史SpringBoot的魅力SpringBoot的優(yōu)點(diǎn)總結(jié)Spring?Boot是一個基于Spring框架的快速開發(fā)腳手架,它簡化了Spring應(yīng)用的初始化和搭建過程,需要的朋友可以參考下2023-09-09
java拷貝指定目錄下所有內(nèi)容到minIO代碼實(shí)例
這篇文章主要介紹了java拷貝指定目錄下所有內(nèi)容到minIO代碼實(shí)例,創(chuàng)建桶 直接使用工具類先判斷,再創(chuàng)建即可,創(chuàng)建文件夾,需要注意以"/"結(jié)尾,實(shí)際也是在minIO上創(chuàng)建文件,只是作為目錄的表現(xiàn)形式展示,需要的朋友可以參考下2024-01-01
Spring?MVC中JSON數(shù)據(jù)處理方式實(shí)戰(zhàn)案例
Spring MVC是個靈活的框架,返回JSON數(shù)據(jù)的也有很多五花八門的方式,下面這篇文章主要給大家介紹了關(guān)于Spring?MVC中JSON數(shù)據(jù)處理方式的相關(guān)資料,需要的朋友可以參考下2024-01-01
JAVA8 STREAM COLLECT GROUPBY分組實(shí)例解析
這篇文章主要介紹了JAVA8 STREAM COLLECT GROUPBY分組實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01
深入講解spring boot中servlet的啟動過程與原理
這篇文章主要給大家介紹了關(guān)于spring boot中servlet啟動過程與原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07

