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。
案例分析:
1、创建相应的Java类
1.1创建一个CollectionBean存放Java Collections types List、Set、Map and Properties集合对象。
package www.csdn.spring.collection.set;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CollectionBean {
// set集合
public Set<String> sets;
public void setSets(Set<String> sets) {
this.sets = sets;
}
// list集合
public List<User> users;
public void setUsers(List<User> users) {
this.users = users;
}
// map集合
public Map<Integer, User> map;
public void setMap(Map<Integer, User> map) {
this.map = map;
}
// props集合
public Properties props;
public void setProps(Properties props) {
this.props = props;
}
}
1.2 在上类中使用到User类,User的代码如下:
package www.csdn.spring.collection.set;
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
2、在spring-collection.xml文件中配置bean
<?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="collectionBean" class="www.csdn.spring.collection.set.CollectionBean"
scope="singleton" lazy-init="default">
<!-- set集合 -->
<property name="sets">
<set>
<value>陈红军</value>
<value>军哥</value>
</set>
</property>
<!-- list集合 -->
<property name="users">
<!-- 采用array配置 -->
<array>
<ref bean="u1" />
<ref bean="u2" />
</array>
<!-- 或者采用list配置-->
<!--
<list>
<ref bean="u1"/>
<ref bean="u2"/>
</list>
-->
</property>
<!-- map集合 -->
<property name="map">
<map>
<entry key="1" value-ref="u1" />
<entry key="2">
<ref bean="u2" />
</entry>
</map>
</property>
<!-- props集合 -->
<property name="props">
<props>
<prop key="1">jdbc:oracle</prop>
<prop key="2">jdbc:mysql</prop>
<prop key="3">jdbc:access</prop>
</props>
</property>
</bean>
<!-- User实体Bean的配置 -->
<bean id="u1" class="www.csdn.spring.collection.set.User">
<property name="name" value="陈红均" />
<property name="age" value="28" />
</bean>
<bean id="u2" class="www.csdn.spring.collection.set.User">
<property name="name" value="信心套" />
<property name="age" value="28" />
</bean>
</beans>
3、创建测试类 测试代码如下:
@Test
public void testSets() {
//创建应用上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-collection.xml");
//根据上下文对象的getBean方法获取指定的Bean对象
CollectionBean bean = context.getBean("collectionBean",CollectionBean.class);
System.out.println("-----------------------set------------------------------");
// 获取set集合
Set<String> sets = bean.sets;
// 得到迭代器
Iterator<String> it = sets.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("-----------------------list------------------------------");
List<User> users = bean.users;
for (User u : users) {
System.out.println(u.getName() + "-------------" + u.getAge());
}
//map第一种遍历方式
System.out.println("-----------------------map1------------------------------");
Map<Integer, User> map = bean.map;
// 得到map集合的key键值的set集合
Set<Integer> setkeys = map.keySet();
// 得到key键值set集合的迭代器
Iterator<Integer> itkeys = setkeys.iterator();
// 迭代键值
while (itkeys.hasNext()) {
// 得到一个具体的键值
Integer key = itkeys.next();
// 通过map集合的get(key)方法 获取key键值对应的value值
User user = map.get(key);
System.out.println(key + "===========" + user.getName()+ "======" + user.getAge());
}
// map第二种遍历方式
System.out.println("----------------------------map2-----------------------------------");
// 获取实体对象的set集合
Set<Entry<Integer, User>> setentry = map.entrySet();
// 获取实体对象的迭代器
Iterator<Entry<Integer, User>> itentry = setentry.iterator();
// 迭代
while (itentry.hasNext()) {
// 得到具体的Entry对象
Entry<Integer, User> entry = itentry.next();
// 通过entry对象的getKey() 和getValue分别得到key与value值
System.out.println(entry.getKey() + "======="+entry.getValue().getName() + "====="+ entry.getValue().getAge());
}
System.out.println("--------------------------props---------------------------");
Properties props = bean.props;
//得到这个集合键值的key的set集合
Set<String> setprops = props.stringPropertyNames();
//key集合迭代器
Iterator<String> keystr = setprops.iterator();
while(keystr.hasNext()){
//具体键值
String key = keystr.next();
//getProperty(key)获取key对应的value值
System.out.println(key+"-------------"+props.getProperty(key));
}
}
4、执行结果