3.4.2.4 Collections
In the <list/>
, <set/>
, <map/>
, and <props/>
elements, you set the properties and arguments of the Java Collection
types List
, Set
, Map
, and Properties
, respectively.
<bean id="moreComplexObject" class="example.ComplexObject"> <!-- results in a setAdminEmails(java.util.Properties) call --> <property name="adminEmails"> <props> <prop key="administrator">administrator@example.org</prop> <prop key="support">support@example.org</prop> <prop key="development">development@example.org</prop> </props> </property> <!-- results in a setSomeList(java.util.List) call --> <property name="someList"> <list> <value>a list element followed by a reference</value> <ref bean="myDataSource" /> </list> </property> <!-- results in a setSomeMap(java.util.Map) call --> <property name="someMap"> <map> <entry key="an entry" value="just some string"/> <entry key ="a ref" value-ref="myDataSource"/> </map> </property> <!-- results in a setSomeSet(java.util.Set) call --> <property name="someSet"> <set> <value>just some string</value> <ref bean="myDataSource" /> </set> </property> </bean>
The value of a map key or value, or a set value, can also again be any of the following elements:
具体例子如下:
package com.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.List;
public class UserServiceImplement implements IUserService {
public Set<String> getS() {
return s;
}
public void setS(Set<String> s) {
this.s = s;
}
public Map<String, String> getM() {
return m;
}
public void setM(Map<String, String> m) {
this.m = m;
}
public Properties getP() {
return p;
}
public void setP(Properties p) {
this.p = p;
}
public List<String> getL() {
return l;
}
public void setL(List<String> l) {
this.l = l;
}
private Set<String> s = new HashSet<String>();
private Map<String, String> m = new HashMap<String, String>();
private Properties p = new Properties();
private List<String> l = new ArrayList<String>();
public void saveUser() {
System.out.println("Set集合注入");
for (String str : s) {
System.out.println(str);
}
System.out.println("------------------------------");
System.out.println("Map集合注入");
for (String str : m.values()) {
System.out.println(str);
}
System.out.println("------------------------------");
System.out.println("Properties属性集合注入");
for (Object str : p.values()) {
System.out.println(str);
}
System.out.println("------------------------------");
System.out.println("List集合注入");
for (String str : l) {
System.out.println(str);
}
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<bean id="userservice" class="com.test.UserServiceImplement">
<property name="s">
<set>
<value>SetValue1</value>
<value>SetValue2</value>
<value>SetValue3</value>
</set>
</property>
<property name="m">
<map>
<entry key="MapKey1" value="MapValue1"></entry>
<entry key="MapKey2" value="MapValue2"></entry>
<entry key="MapKey3" value="MapValue3"></entry>
</map>
</property>
<property name="p">
<props>
<prop key="PropertiesKey1">PropertiesValue1</prop>
<prop key="PropertiesKey2">PropertiesValue2</prop>
<prop key="PropertiesKey3">PropertiesValue3</prop>
</props>
</property>
<property name="l">
<list>
<value>ListValue1</value>
<value>ListValue2</value>
<value>ListValue3</value>
</list>
</property>
</bean>
</beans>
测试代码:
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"com/test/bean.xml");
IUserService us = (IUserService) ctx.getBean("userservice");
us.saveUser();
}
}