依賴注入的兩種常用方式(構造器和Setter)與注入內容(裝配資料)——Spring IOC/DI(三)
本章主要講解一下Spring中依賴注入方式,接上一章依賴注入原理和方式:
https://blog.csdn.net/qq_34598667/article/details/83315669
依賴注入常用的兩種方式以及注入的內容(裝配資料)
Spring通過DI(依賴注入)實現IOC(控制反轉),常用的注入方式主要有三種:構造方法注入,setter注入和介面注入。
本章重點講解基本的兩種方式構造器和setter,註解後面會單獨一章講解
案例準備
建立ApplicationContext.xml作為Spring配置檔案:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<!-- bean ... -->
</beans>
在新建一個包,com.oak.entity,新建實體類Personh和Man如下:
public class Person {
private String name;
private Integer age;
//... 構造方法,getter,setter和toString方法略
}
public class Man {
private Person person;
//... 構造方法,getter,setter和toString方法略
}
新建一個com.oak.test,新建一個DITest類
構造器注入
容器呼叫帶有一組引數的類構造方法完成依賴注入,使用的是〈bean〉標籤中的〈constructor-arg〉元素
在xml中新增bean,使用構造器注入屬性值,使用value
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<!-- 構造器注入 -->
<bean id="person" class="com.oak.entity.Person">
<constructor-arg value="二狗"/>
<constructor-arg value="18"/>
</bean>
</beans>
在DUTest類中新增測試方法test01並測試:
@Test
public void test01(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=ctx.getBean("person",Person.class);
System.out.println(person);
}
檢視控制檯輸出:
Person [name=二狗, age=18]
1)多個引數時,把引數傳遞給建構函式可能會存在歧義
可以使用 index 屬性來顯式的指定建構函式引數的索引,索引從0開始
修改< bean >定義如下:
<bean id="person" class="com.oak.entity.Person">
<constructor-arg index="0" value="二狗"/>
<constructor-arg index="1" value="18"/>
</bean>
2)使用type屬性指定引數的型別
注意:基本型別有包裝型別需要進行區分
修改< bean >定義如下:
<bean id="person" class="com.oak.entity.Person">
<constructor-arg index="0" type="java.lang.String" value="二狗"/>
<constructor-arg index="1" type="java.lang.Integer" value="18"/>
</bean>
3)需要給引數指定一個引用(指向其他bean),使用ref屬性
修改xml。新增一個bean定義
<bean id="man" class="com.oak.entity.Man">
<constructor-arg ref="person"/>
</bean>
修改test01並測試:
@Test
public void test01(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=ctx.getBean("person",Person.class);
System.out.println(person);
Man man=ctx.getBean("man",Man.class);
System.out.println(man);
}
控制檯輸出結果:
Person [name=二狗, age=18]
Man [person=Person [name=二狗, age=18]]
setter方法注入
當容器呼叫一個無參的建構函式或一個無參的靜態工廠方法來初始化bean,通過容器在bean 上呼叫setter設值函式 ,使用的是〈bean〉標籤中的〈property〉元素
property的name屬性指定類的屬性名,需要一致,其他與構造器相同
在xml中新增一個新的bean
<!-- setter方法注入 -->
<bean id="personSet" class="com.oak.entity.Person">
<property name="age" value="18"/>
<property name="name" value="二蛋"/>
</bean>
在測試類中新加一個test02方法
@Test
public void test02(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=ctx.getBean("personSet",Person.class);
System.out.println(person);
}
測試並檢視控制檯:
Person [name=二蛋, age=18]
注入的內容(裝配資料)
1)引數值注入
上面已經講過可以通過value進行引數值注入,使用ref進行bean物件的注入
2)注入集合
< list >:注入一列值,允許重複
< set >:注入一列值,不允許重複
< map >:注入鍵(名)值對的集合,名稱和值可以是任何型別
< props >:注入鍵(名)值對的集合,名稱和值可以是任何類
例:在com.oak.entity包中新建JavaCollection類如下:
public class JavaCollection {
private List list;
private Map map;
private Properties prop;
//構造方法,setter,getter和toString方法略
}
在Spring配置中定義bean,修改xml,新增bean定義
<bean id="coll" class="com.oak.entity.JavaCollection">
<property name="list">
<!-- 注入集合 值可重複 set就不舉例了-->
<list>
<value>list1</value>
<value>list1</value>
</list>
</property>
<property name="map">
<!-- 注入map -->
<map>
<entry key="二蛋">
<value>18</value>
</entry>
</map>
</property>
<property name="prop">
<!-- 注入properties -->
<props>
<prop key="二黑">男</prop>
</props>
</property>
</bean>
在測試類中新加測試方法test03並測試:
@Test
public void test03(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
JavaCollection collection=ctx.getBean("coll",JavaCollection.class);
System.out.println(collection);
}
檢視控制太輸出:
JavaCollection [list=[list1, list1], map={二蛋=18}, prop={二黑=男}]
注入Spring表示式
Sprng引入一種跟EL表示式型別語法的Spring表示式,可以讀取一個bean物件或者集合中的內容:
#{bean.屬性}
在Spring配置中定義bean,修改xml,新增bean定義
<!-- setter方法注入 -->
<bean id="personSet1" class="com.oak.entity.Person">
<property name="age" value="#{personSet.age}"/>
<property name="name" value="#{personSet.name}"/>
</bean>
在測試類中新增test04並測試
@Test
public void test04(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=ctx.getBean("personSet",Person.class);
System.out.println(person);
Person person1=ctx.getBean("personSet1",Person.class);
System.out.println(person1);
}
檢視控制檯結果
Person [name=二蛋, age=18]
Person [name=二蛋, age=18]
注入空字串或者null
""和< null />
就不做案例了
下一章:無註解的自動裝配以及方式
https://blog.csdn.net/qq_34598667/article/details/83317377