下面例子向您展示Spring如何注入值到集合类型(List, Set, Map, and Properties)。 支持4个主要的集合类型:
- List – <list/>
- Set – <set/>
- Map – <map/>
- Properties – <props/>
Spring beans
一个Customer对象,有四个集合属性。
File:Customer.java
public class Customer {
private List<Object> lists;
private Set<Object> sets;
private Map<Object,Object> maps;
private Properties pros;
public void setLists(List<Object> lists) {
this.lists = lists;
}
public void setSets(Set<Object> sets) {
this.sets = sets;
}
public void setMaps(Map<Object, Object> maps) {
this.maps = maps;
}
public void setPros(Properties pros) {
this.pros = pros;
}
@Override
public String toString() {
return "Customer [\n lists=" + lists + ",\n sets=" + sets + ",\n maps=" + maps + ",\n pros=" + pros + "\n]";
}
}
File:Person.java
public class Person {
private String name;
private String address;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", address=" + address + ", age=" + age + "]";
}
}
在bean配置文件中不同的代码片段用来声明集合。
1. List示例
<!-- java.util.List -->
<property name="lists">
<list>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.ray.common.Person">
<property name="name" value="Rain" />
<property name="address" value="zhuhaiGongbei" />
<property name="age" value="17" />
</bean>
</list>
</property>
2. Set示例
<!-- java.util.Set -->
<property name="sets">
<set>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.ray.common.Person">
<property name="name" value="Rain" />
<property name="address" value="zhuhaiGongbei" />
<property name="age" value="17" />
</bean>
</set>
</property>
3. Map示例
<!-- java.util.Map -->
<property name="maps">
<map>
<entry key="Key 1" value="1" />
<entry key="Key 2" value-ref="PersonBean" />
<entry key="Key 3">
<bean class="com.ray.common.Person">
<property name="name" value="Rain" />
<property name="address" value="zhuhaiGongbei" />
<property name="age" value="17" />
</bean>
</entry>
</map>
</property>
4. Properties示例
<!-- java.util.Properties -->
<property name="pros">
<props>
<prop key="admin">admin@ray.com</prop>
<prop key="super">super@ray.com</prop>
</props>
</property>
<?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="CustomerBean" class="com.ray.common.Customer">
<!-- java.util.List -->
<property name="lists">
<list>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.ray.common.Person">
<property name="name" value="Rain" />
<property name="address" value="zhuhaiGongbei" />
<property name="age" value="17" />
</bean>
</list>
</property>
<!-- java.util.Set -->
<property name="sets">
<set>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.ray.common.Person">
<property name="name" value="Rain" />
<property name="address" value="zhuhaiGongbei" />
<property name="age" value="17" />
</bean>
</set>
</property>
<!-- java.util.Map -->
<property name="maps">
<map>
<entry key="Key 1" value="1" />
<entry key="Key 2" value-ref="PersonBean" />
<entry key="Key 3">
<bean class="com.ray.common.Person">
<property name="name" value="Rain" />
<property name="address" value="zhuhaiGongbei" />
<property name="age" value="17" />
</bean>
</entry>
</map>
</property>
<!-- java.util.Properties -->
<property name="pros">
<props>
<prop key="admin">admin@ray.com</prop>
<prop key="super">super@ray.com</prop>
</props>
</property>
</bean>
<bean id="PersonBean" class="com.ray.common.Person">
<property name="name" value="Ray" />
<property name="address" value="zhuhai" />
<property name="age" value="17" />
</bean>
</beans>
执行程序
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "beans2.xml" });
Customer cust = (Customer) context.getBean("CustomerBean");
System.out.println(cust);
}
}
执行结果
Customer [
lists=[1, Person [name=Ray, address=zhuhai, age=17], Person [name=Rain, address=zhuhaiGongbei, age=17]],
sets=[1, Person [name=Ray, address=zhuhai, age=17], Person [name=Rain, address=zhuhaiGongbei, age=17]],
maps={Key 1=1, Key 2=Person [name=Ray, address=zhuhai, age=17], Key 3=Person [name=Rain, address=zhuhaiGongbei, age=17]},
pros={admin=admin@ray.com, super=super@ray.com}
]