Spring 构造函数注入和Setter方法注入及集合注入
本文内容纲要:
- 1.简介- 2. 构造函数注入
- 3. Setter方法注入
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 - listNames; - /**注入Set集合/ - private Set - 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 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 - getListNames() { - return listNames; - } - public void setListNames(List - listNames) { - this.listNames = listNames; - } - public Set - getSetNames() { - return setNames; - } - public void setSetNames(Set - 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 
- 新建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配置信息 - 张三 - 李四 - 王五 - <!--注入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> 
原文地址:https://blog.csdn.net/lyc_liyanchao/article/details/82428726
本文内容总结:1.简介,2. 构造函数注入,3. Setter方法注入,
原文链接:https://www.cnblogs.com/w2116o2115/p/12706120.html
以上是 Spring 构造函数注入和Setter方法注入及集合注入 的全部内容, 来源链接: utcz.com/z/295997.html








