Spring构造函数注⼊和Setter⽅法注⼊及集合注⼊
1.简介
Spring的依赖注⼊⽅式⼤体上可以分为三种:
构造函数注⼊
Setter⽅法注⼊
⽅法注⼊(lookup-method注⼊和replace-method注⼊)
本篇我们先分析构造函数注⼊和Setter⽅法注⼊,并简介⼀下Spring中的集合属性,Properties属性,数组属性等注⼊,⽅法注⼊稍微复杂且不常⽤,我们留在下篇分析。
2. 构造函数注⼊
新建HelloApi接⼝
package com.lyc.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.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;
}
/**
* 打印数组集合
*/spring framework扩展点
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 : Set()) {
System.out.println("key= " + Key() + " value= " + Value());            }
System.out.println();
}
}
/**
* 打印Properties属性
*/
public void sayPropertiesNames() {
if (null != propertiesNames) {
System.out.println("注⼊properties属性-->");
System.out.("name"));
System.out.("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.());
}
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;
}
}
新建l
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd">
<!-- ====================构造器属性注⼊(静态⼯⼚和实例⼯⼚属性注⼊⽅式与构造器⽅式注⼊相同)Begin==================== --> <!-- 通过构造器参数索引⽅式依赖注⼊ -->
<bean id="helloBeanByIndex" class="com.lyc.day04.HelloImpl">
<constructor-arg index="0" value="⼩张"/>
<constructor-arg index="1" value="3"/>
</bean>
<!-- 通过构造器参数类型⽅式依赖注⼊ -->
<bean id="helloBeanByType" class="com.lyc.day04.HelloImpl">
<constructor-arg type="java.lang.String" value="⼩李"/>
<constructor-arg type="int" value="4"/>
</bean>
<!-- 通过构造器参数名称⽅式依赖注⼊ -->
<bean id="helloBeanByName" class="com.lyc.day04.HelloImpl">
<constructor-arg name="name" value="⼩王"/>
<constructor-arg name="age" value="5"/>
</bean>
<!-- ====================构造器属性注⼊End==================== -->
</beans>
新建MyTest测试类
package com.lyc.day04;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.l.XmlBeanFactory;
import 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("l"));
}
@After
public void after() {
System.out.println("\n========测试⽅法结束=======");
}
/**
* 构造函数注⼊
*/
@Test
public void testConstructorDI() {
helloApi = Bean("helloBeanByIndex", HelloImpl.class);
helloApi.sayHello();
helloApi = Bean("helloBeanByType", HelloImpl.class);
helloApi.sayHello();
helloApi = Bean("helloBeanByName", HelloImpl.class);
helloApi.sayHello();
}
}
输出
========测试⽅法开始=======
⼤家好, 我叫⼩张, 我今年3岁了
⼤家好, 我叫⼩李, 我今年4岁了
⼤家好, 我叫⼩王, 我今年5岁了
========测试⽅法结束=======
可以看到,构造函数也分为了三种类型指定参数索引,指定参数类型,指定参数名称三种⽅式,当我们指定任意⼀种⽅式时,Spring会⾃动帮我们识别对应的构造函数,并将值进⾏注⼊,这⼀部分的实现在Spring源码中的实现还是很复杂的,会在接下来的博客中进⾏详细分析。
3. Setter⽅法注⼊
  Setter⽅法注⼊是最简单也是最基础的⼀种注⼊⽅式,在本⼩节我们会分析普通属性注⼊,数组属性注
⼊,List属性注⼊,Map类型属性注
⼊,Properties类型属性注⼊等⽅式
修改l增加bean配置信息
<!-- ====================Setter⽅法属性注⼊Begin==================== -->
<bean id="helloBeanBySetter" class="com.lyc.day04.HelloImpl">
<property name="name" value="⼩明"/>
<property name="age" value="3"/>
</bean>
<bean id="helloBeanBySetterCollection" class="com.lyc.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==================== -->原⽂地址:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。