一个user类
package www.xxxx.spring.dao;
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;
}
}
CollectionBean类
package www.xxxx.spring.dao;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CollectionBean {
public Set sets;
//set集合依赖注入
public void setSets(Set sets) {
this.sets = sets;
}
public List<User> users;
//list集合依赖注入
public void setUsers(List<User> users) {
this.users = users;
}
public Properties props;
//Properties集合依赖注入
public void setProps(Properties props) {
this.props = props;
}
public Map<Integer,User> map;
//Map集合依赖注入
public void setMap(Map<Integer, User> map) {
this.map = map;
}
}
spring-CollectionBean.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="collectionBean" class="www.xxxx.spring.dao.CollectionBean" scope="singleton" lazy-init="default">
<property name="sets">
<set>
<value>张三</value>
<value>李四</value>
<value>王五</value>
<value>赵六</value>
<value>钱八</value>
</set>
</property>
<property name="users">
<list>
<ref bean="u1"/>
<ref bean="u2"/>
<ref bean="u3"/>
<ref bean="u4"/>
</list>
<!-- <array>
<ref bean="u1"/>
<ref bean="u2"/>
<ref bean="u3"/>
<ref bean="u4"/>
</array> -->
</property>
<property name="map">
<map>
<entry key="1" value-ref="u1"/>
<entry key="2">
<ref bean ="u2"/>
</entry>
<entry key="3" value-ref="u3"/>
</map>
</property>
<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>
<bean id="u1" class="www.xxxx.spring.dao.User">
<property name="name" value="zhang" />
<property name="age" value="22"></property>
</bean>
<bean id="u2" class="www.xxxx.spring.dao.User">
<property name="name" value="zhang" />
<property name="age" value="22"></property>
</bean>
<bean id="u3" class="www.xxxx.spring.dao.User">
<property name="name" value="zhang" />
<property name="age" value="22"></property>
</bean>
<bean id="u4" class="www.xxxx.spring.dao.User">
<property name="name" value="zhang" />
<property name="age" value="22"></property>
</bean>
</beans>
测试类
package www.xxxx.spring.dao;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBean {
@Test
public void testSets(){
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring-CollectionBean.xml");
CollectionBean bean = context.getBean("collectionBean",CollectionBean.class);
//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 user:users){
System.out.println(user.getName()+"sss"+user.getAge());
}
System.out.println("-----------------------");
//map集合
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键值对应的value值
User user = map.get(key);
System.out.println(key+"dddddd"+user.getName()+"sss"+user.getAge());
}
System.out.println("sdfd-----------------");
//map集合2
//获取实体对象的set集合
Set<Entry<Integer, User>> setentry = map.entrySet();
//获取实体对象的迭代器
Iterator<Entry<Integer,User>> itentry = setentry.iterator();
while(itentry.hasNext()){
Entry<Integer,User> entry = itentry.next();
//通过
System.out.println(entry.getKey()+"sssaaa"+entry.getValue().getName()+"sssdd"+entry.getValue().getAge());
}
System.out.println("=========================");
//Properties集合
Properties props= bean.props;
//得到这个结合键值的Key的set集合
Set<String> setprops = props.stringPropertyNames();
//String 集合迭代
Iterator<String> keystr = setprops.iterator();
while(keystr.hasNext()){
String key = keystr.next();
System.out.println(key+"ssssssssss"+props.getProperty(key));
}
}
}