Spring点滴八:Spring注入集合

时间:2022-09-06 21:56:55

在Spring中我们通过value属性来配置基本数据类型,通过标签的ref属性来配置对象的引用。这两种情况只能给bean传递一个值,那么如何传递多个值呢?Spring提供了四种Collection类型集合配置元素。

元素 描述
<List> 它有助于连线,如注入一列值,允许重复
<set> 它有助于连线一组值,但不能重复
<map> 它可以用来注入名称-值对的集合,其中名称和值可以是任何类型
<props> 它可以用来注入名称-值对的集合,其中名称和值都是字符串类型

Spring.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"> <bean id="collection" class="com.test.spring.Collection">
<property name="list">
<list>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</list>
</property>
<property name="set">
<set>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</set>
</property>
<property name="map">
<map>
<entry key="1" value="张三"></entry>
<entry key="2" value="李四"></entry>
<entry key="3" value="王五"></entry>
</map>
</property>
<property name="proper">
<props>
<prop key="1">张三</prop>
<prop key="2">李四</prop>
<prop key="3">王五</prop>
</props>
</property>
</bean>
</beans>

Java代码:

package com.test.spring;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; public class Collection {
private List list;
private Set set;
private Map map;
private Properties proper; public Properties getProper() {
return proper;
} public void setProper(Properties proper) {
this.proper = proper;
} public List getList() {
return list;
} public void setList(List list) {
this.list = list;
} public Set getSet() {
return set;
} public void setSet(Set set) {
this.set = set;
} public Map getMap() {
return map;
} public void setMap(Map map) {
this.map = map;
}
}

测试:

package com.test.spring;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class T {
AbstractApplicationContext applicationcontext=null;
@Before
public void before() {
System.out.println("》》》Spring ApplicationContext容器开始初始化了......");
applicationcontext= new ClassPathXmlApplicationContext(new String[]{"test1-service.xml"});
System.out.println("》》》Spring ApplicationContext容器初始化完毕了......");
}
@Test
public void test() {
collection=applicationcontext.getBean(Collection.class);
System.out.println(collection.getList());
System.out.println(collection.getSet());
System.out.println(collection.getMap());
System.out.println(collection.getProper());
}
}

测试结果:

》》》Spring ApplicationContext容器开始初始化了......
2017-03-19 15:49:39  INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5facc36f: startup date [Sun Mar 19 15:49:39 CST 2017]; root of context hierarchy
2017-03-19 15:49:40  INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
》》》Spring ApplicationContext容器初始化完毕了......
[张三, 李四, 王五]
[张三, 李四, 王五]
{1=张三, 2=李四, 3=王五}
{3=王五, 2=李四, 1=张三}