1.簡介
Spring的依賴注入方式大體上可以分為三種:
- 構造函數注入
- Setter方法注入
- 方法注入 (lookup-method注入和replace-method注入)
本篇我們先分析構造函數注入和Setter方法注入,並簡介一下Spring中的集合屬性,Properties屬性,數組屬性等注入,方法注入稍微復雜且不常用,我們留在下篇分析。
2. 構造函數注入
- 新建HelloApi接口
package com.lyc.cn.day04; /** * @author: LiYanChao * @create: 2018-09-05 11:49 */ public interface HelloApi { void sayHello(); void sayListNames(); void saySetNames(); void sayArrayNames(); void sayMapNames(); void sayPropertiesNames(); }
- 新建HelloImpl實現類
package com.lyc.cn.day04; import org.springframework.util.CollectionUtils; import java.util.*; /** * @author: LiYanChao * @create: 2018-09-05 11:49 */ public class HelloImpl implements HelloApi { /** 姓名 **/ private String name; /** 年齡 **/ private int age; /** 注入List集合 **/ private List<String> listNames; /***注入Set集合*/ private Set<String> setNames; /** 注入Properties **/ private Properties propertiesNames; /** 注入Map集合 **/ private Map<String, String> mapNames; /** 注入數組 **/ private String[] arrayNames; /** 無參構造函數 **/ public HelloImpl() { } /** * 構造函數 * @param name 姓名 * @param age 年齡 */ public HelloImpl(String name, int age) { this.name = name; this.age = age; } /** * 打印數組集合 */ public void sayArrayNames() { System.out.println("注入數組-->"); if (arrayNames != null && arrayNames.length > 0) { for (int i = 0; i < arrayNames.length; i++) { System.out.println(arrayNames[i]); } } System.out.println(); } /** * 打印Map集合 */ public void sayMapNames() { if (null != mapNames && mapNames.size() > 0) { System.out.println("注入Map集合-->"); for (Map.Entry<String, String> entry : mapNames.entrySet()) { System.out.println("key= " + entry.getKey() + " value= " + entry.getValue()); } System.out.println(); } } /** * 打印Properties屬性 */ public void sayPropertiesNames() { if (null != propertiesNames) { System.out.println("注入properties屬性-->"); System.out.println(propertiesNames.get("name")); System.out.println(propertiesNames.get("age")); System.out.println(); } } /** * 打印List集合 */ public void sayListNames() { if (!CollectionUtils.isEmpty(listNames)) { System.out.println("注入List集合-->"); for (String string : listNames) { System.out.println(string); } System.out.println(); } } /** * 打印Set集合 */ public void saySetNames() { if (!CollectionUtils.isEmpty(setNames)) { System.out.println("注入Set集合-->"); Iterator<String> iterator = setNames.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } System.out.println(); } } @Override public void sayHello() { System.out.println("大家好, 我叫" + getName() + ", 我今年" + getAge() + "歲了"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<String> getListNames() { return listNames; } public void setListNames(List<String> listNames) { this.listNames = listNames; } public Set<String> getSetNames() { return setNames; } public void setSetNames(Set<String> setNames) { this.setNames = setNames; } public Properties getPropertiesNames() { return propertiesNames; } public void setPropertiesNames(Properties propertiesNames) { this.propertiesNames = propertiesNames; } public Map<String, String> getMapNames() { return mapNames; } public void setMapNames(Map<String, String> mapNames) { this.mapNames = mapNames; } public String[] getArrayNames() { return arrayNames; } public void setArrayNames(String[] arrayNames) { this.arrayNames = arrayNames; } }
- 新建day04.xml
<?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"> <!-- ====================構造器屬性注入(靜態工廠和實例工廠屬性注入方式與構造器方式注入相同)Begin==================== --> <!-- 通過構造器參數索引方式依賴注入 --> <bean id="helloBeanByIndex" class="com.lyc.cn.day04.HelloImpl"> <constructor-arg index="0" value="小張"/> <constructor-arg index="1" value="3"/> </bean> <!-- 通過構造器參數類型方式依賴注入 --> <bean id="helloBeanByType" class="com.lyc.cn.day04.HelloImpl"> <constructor-arg type="java.lang.String" value="小李"/> <constructor-arg type="int" value="4"/> </bean> <!-- 通過構造器參數名稱方式依賴注入 --> <bean id="helloBeanByName" class="com.lyc.cn.day04.HelloImpl"> <constructor-arg name="name" value="小王"/> <constructor-arg name="age" value="5"/> </bean> <!-- ====================構造器屬性注入End==================== --> </beans>
- 新建MyTest測試類
package com.lyc.cn.day04; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; /** * @author: LiYanChao * @create: 2018-09-05 11:49 */ public class MyTest { private XmlBeanFactory xmlBeanFactory; private HelloApi helloApi; @Before public void initXmlBeanFactory() { System.out.println("========測試方法開始=======\n"); xmlBeanFactory = new XmlBeanFactory(new ClassPathResource("day04.xml")); } @After public void after() { System.out.println("\n========測試方法結束======="); } /** * 構造函數注入 */ @Test public void testConstructorDI() { helloApi = xmlBeanFactory.getBean("helloBeanByIndex", HelloImpl.class); helloApi.sayHello(); helloApi = xmlBeanFactory.getBean("helloBeanByType", HelloImpl.class); helloApi.sayHello(); helloApi = xmlBeanFactory.getBean("helloBeanByName", HelloImpl.class); helloApi.sayHello(); } }
- 輸出
========測試方法開始=======
大家好, 我叫小張, 我今年3歲了
大家好, 我叫小李, 我今年4歲了
大家好, 我叫小王, 我今年5歲了
========測試方法結束=======
可以看到,構造函數也分為了三種類型指定參數索引
,指定參數類型
,指定參數名稱
三種方式,當我們指定任意一種方式時,Spring會自動幫我們識別對應的構造函數,並將值進行注入,這一部分的實現在Spring源碼中的實現還是很復雜的,會在接下來的博客中進行詳細分析。
3. Setter方法注入
Setter方法注入是最簡單也是最基礎的一種注入方式,在本小節我們會分析普通屬性注入
,數組屬性注入
,List屬性注入
,Map類型屬性注入
,Properties類型屬性注入
等方式
- 修改day04.xml增加bean配置信息
<!-- ====================Setter方法屬性注入Begin==================== --> <bean id="helloBeanBySetter" class="com.lyc.cn.day04.HelloImpl"> <property name="name" value="小明"/> <property name="age" value="3"/> </bean> <bean id="helloBeanBySetterCollection" class="com.lyc.cn.day04.HelloImpl"> <!--注入List集合--> <property name="listNames"> <!-- merge 父子bean是否合並條目 --> <list value-type="java.lang.String" merge="false"> <value>張三</value> <value>李四</value> <value>王五</value> </list> </property> <!--注入Set集合--> <property name="setNames"> <set value-type="java.lang.String" merge="true"> <value>張三</value> <value>李四</value> <value>王五</value> </set> </property> <!--注入Map集合--> <property name="mapNames"> <map key-type="java.lang.String" value-type="java.lang.String"> <entry key="name" value="小明"/> <entry key="age" value="3"/> </map> </property> <!--注入數組--> <property name="arrayNames"> <array value-type="java.lang.String"> <value>張三</value> <value>李四</value> <value>王五</value> </array> </property> <!--注入Properties--> <property name="propertiesNames"> <props value-type="java.lang.String"> <prop key="name">小明</prop> <prop key="age">3</prop> </props> </property> </bean> <!-- ====================Setter方法屬性注入End==================== -->
原文地址:https://blog.csdn.net/lyc_liyanchao/article/details/82428726