Spring 3支持4种集合类型:
- List – <list/>
- Set – <set/>
- Map – <map/>
- Properties – <props/>
下面是一个Customer对象,带有四个集合属性。
package com.xuejava.common;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Customer {
private List<Object> lists;
private Set<Object> sets;
private Map<Object, Object> maps;
private Properties pros;
//...
}
1、List示例
在bean配置文件中声明List集合。
<property name="lists">
<list>
<value>1</value>
<ref bean="PersonBean" />
<bean class="cn.xuejava.common.Person">
<property name="name" value="xuejavaList" />
<property name="address" value="address" />
<property name="age" value="28" />
</bean>
</list>
</property>
2、 Set示例
在bean配置文件中声明Set集合。
<property name="sets">
<set>
<value>1</value>
<ref bean="PersonBean" />
<bean class="cn.xuejava.common.Person">
<property name="name" value="xuejavaSet" />
<property name="address" value="address" />
<property name="age" value="28" />
</bean>
</set>
</property>
3、 Map示例
在bean配置文件中声明Map集合。
<property name="maps">
<map>
<entry key="Key 1" value="1" />
<entry key="Key 2" value-ref="PersonBean" />
<entry key="Key 3">
<bean class="cn.xuejava.common.Person">
<property name="name" value="xuejavaMap" />
<property name="address" value="address" />
<property name="age" value="28" />
</bean>
</entry>
</map>
</property>
4、 Properties示例
在bean配置文件中声明Properties。
<property name="pros">
<props>
<prop key="admin">admin@163.com</prop>
<prop key="support">support@163.com</prop>
</props>
</property>
5、 完整的Spring bean配置文件
完整的Spring-Customer.xml配置文件如下:
<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-3.0.xsd">
<bean id="CustomerBean" class="cn.xuejava.common.Customer">
<!-- java.util.List -->
<property name="lists">
<list>
<value>1</value>
<ref bean="PersonBean" />
<bean class="cn.xuejava.common.Person">
<property name="name" value="xuejavaList" />
<property name="address" value="address" />
<property name="age" value="28" />
</bean>
</list>
</property>
<!-- java.util.Set -->
<property name="sets">
<set>
<value>1</value>
<ref bean="PersonBean" />
<bean class="cn.xuejava.common.Person">
<property name="name" value="xuejavaSet" />
<property name="address" value="address" />
<property name="age" value="28" />
</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="cn.xuejava.common.Person">
<property name="name" value="xuejavaMap" />
<property name="address" value="address" />
<property name="age" value="28" />
</bean>
</entry>
</map>
</property>
<!-- java.util.Properties -->
<property name="pros">
<props>
<prop key="admin">admin@163.com</prop>
<prop key="support">support@163.com</prop>
</props>
</property>
</bean>
<bean id="PersonBean" class="cn.xuejava.common.Person">
<property name="name" value=" xuejava 1" />
<property name="address" value="address 1" />
<property name="age" value="28" />
</bean>
</beans>
6、 运行
编写测试程序,并运行:
package com.xuejava.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main( String[] args ) {
ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Customer.xml");
Customer cust = (Customer)context.getBean("CustomerBean");
System.out.println(cust);
}
}
7、 输出结果
输出结果如下:
Customer [
lists=[
1,
Person [address=address 1, age=28, name=xuejava 1],
Person [address=address, age=28, name= xuejavaList]
],
maps={
key 1=1,
key 2=Person [address=address 1, age=28, name= xuejava 1],
key 3=Person [address=address, age=28, name= xuejavaMap]
},
pros={admin=admin@163.com, support=support@163.com},
sets=[
1,
Person [address=address 1, age=28, name= xuejava 1],
Person [address=address, age=28, name= xuejavaSet]]
]