上一篇博客我們主要講解了IOC控制反轉,也就是說IOC 讓程序員不在關注怎么去創建對象,而是關注與對象創建之后的操作,把對象的創建、初始化、銷毀等工作交給spring容器來做。那么創建對象的時候,有可能依賴於其他的對象,即類的屬性如何賦值?這也是我們這篇博客講解 Spring 另一個核心要點:DI依賴注入。
PS:本篇博客源碼鏈接:https://pan.baidu.com/s/1kjx5oZRtKjBVXd7OdFPcJQ 密碼:s3e1
1、什么是DI依賴注入?
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就是通過反射來實現注入的。
簡單來說什么是依賴注入,就是給屬性賦值(包括基本數據類型和引用數據類型)
2、利用 set 方法給屬性賦值
第一步:創建工程,並導入相應的 jar 包
第二步:創建實體類 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>
第四步:測試
//利用 set 方法給對象賦值
@Test
public void testSet(){
//1、啟動 spring 容器
//2、從 spring 容器中取出數據
//3、通過對象調用方法
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) context.getBean("person");
System.out.println(person.getPname());//vae
}
3、利用 構造函數 給屬性賦值
第一步:在實體類 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
}
總結:
1、如果spring的配置文件中的bean中沒有<constructor-arg>該元素,則調用默認的構造函數
2、如果spring的配置文件中的bean中有<constructor-arg>該元素,則該元素確定唯一的構造函數
