spring入门-----spring中遍历各种集合

spring-collection.xml

[html]  view plain copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.            http://www.springframework.org/schema/beans/spring-beans.xsd">  
  6.       
  7.     <bean id="collectionBean" class="www.csdn.spring.collection.set.CollectionBean" scope="singleton" lazy-init="default">  
  8.           
  9.         <property name="sets">  
  10.             <set>  
  11.                 <value>岑红军value>  
  12.                 <value>军哥value>  
  13.                 <value>哈哈value>  
  14.                 <value>呵呵value>  
  15.                 <value>嘿嘿value>  
  16.                 <value>洗洗value>  
  17.             set>  
  18.         property>  
  19.          
  20.     <property name="users">  
  21.           
  22.     <property name="map">  
  23.         <map>  
  24.             <entry key="1" value-ref="u1">entry>  
  25.             <entry key="2">  
  26.                 <ref bean="u2"/>  
  27.             entry>  
  28.             <entry key="3" value-ref="u3">  
  29.             entry>  
  30.           
  31.         map>  
  32.     property>  
  33.       
  34.     <property name="props">  
  35.         <props>  
  36.             <prop key="1">jdbc:oracleprop>  
  37.             <prop key="2">jdbc:mysqlprop>  
  38.             <prop key="3">jdbc:accessprop>  
  39.         props>  
  40.     property>  
  41.     bean>  
  42.     <bean id="u1" class="www.csdn.spring.collection.set.User">  
  43.         <property name="name" value="deep" />  
  44.         <property name="age" value="21" />  
  45.     bean>  
  46.     <bean id="u2" class="www.csdn.spring.collection.set.User">  
  47.         <property name="name" value="deepsoul" />  
  48.         <property name="age" value="22">property>  
  49.     bean>  
  50.     <bean id="u3" class="www.csdn.spring.collection.set.User">  
  51.         <property name="name" value="chrp" />  
  52.         <property name="age" value="23" />  
  53.     bean>  
  54.     <bean id="u4" class="www.csdn.spring.collection.set.User">  
  55.         <property name="name" value="redarmy" />  
  56.         <property name="age" value="28" />  
  57.     bean>  
  58.       
  59.       
  60.   
  61. beans>  

User.java

[java]  view plain copy
  1. package www.csdn.spring.collection.set;  
  2.   
  3. public class User {  
  4.     private String name;  
  5.     private Integer age;  
  6.     public void setName(String name) {  
  7.         this.name = name;  
  8.     }  
  9.     public void setAge(Integer age) {  
  10.         this.age = age;  
  11.     }  
  12.       
  13.     public String getName() {  
  14.         return name;  
  15.     }  
  16.     public Integer getAge() {  
  17.         return age;  
  18.     }  
  19.     @Override  
  20.     public String toString() {  
  21.         return "User [name=" + name + ", age=" + age + "]";  
  22.     }  
  23.       
  24.       
  25. }  

Collection.java

[java]  view plain copy
  1. package www.csdn.spring.collection.set;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5. import java.util.Properties;  
  6. import java.util.Set;  
  7.   
  8. public class CollectionBean {  
  9.     //set集合  
  10.         public Set sets;  
  11.   
  12.         public void setSets(Set sets) {  
  13.             this.sets = sets;  
  14.         }  
  15.           
  16.         public CollectionBean() {  
  17.             super();  
  18.             System.out.println("========================");  
  19.         }  
  20.           
  21.         //list集合  
  22.         public List users;  
  23.         public void setUsers(List users) {  
  24.             this.users = users;  
  25.         }  
  26.           
  27.         //map集合  
  28.         public Map map;  
  29.   
  30.         public void setMap(Map map) {  
  31.             this.map = map;  
  32.         }  
  33.           
  34.         //props集合  
  35.         public Properties props;  
  36.   
  37.         public void setProps(Properties props) {  
  38.             this.props = props;  
  39.         }  
  40.           
  41. }  

TestBean.java

[java]  view plain copy
  1. package www.csdn.spring.collection.set;  
  2.   
  3. import java.util.Iterator;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6. import java.util.Map.Entry;  
  7. import java.util.Properties;  
  8. import java.util.Set;  
  9.   
  10. import org.junit.Test;  
  11. import org.springframework.context.ApplicationContext;  
  12. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  13.   
  14. public class TestBean {  
  15.       
  16.     @Test  
  17.     public void tests(){  
  18.         ApplicationContext context = new ClassPathXmlApplicationContext(  
  19.                 "classpath:spring-collection.xml");  
  20.         CollectionBean bean = context.getBean("collectionBean",  
  21.                 CollectionBean.class);  
  22.         System.out.println("---------------set----------------");  
  23.         //获取set集合  
  24.         Set sets=bean.sets;  
  25.         //得到迭代器  
  26.         Iterator it=sets.iterator();  
  27.         //遍历  
  28.         while (it.hasNext()){  
  29.             System.out.println(it.next());  
  30.         }  
  31.           
  32.           
  33.         System.out.println("--------------list----------------");  
  34.         List users = bean.users;  
  35.         for (User u : users) {  
  36.             System.out.println(u.getName() + "-------------" + u.getAge());  
  37.         }  
  38.           
  39.         System.out.println("----------------map1---------------");  
  40.         //map1  
  41.         Map map=bean.map;  
  42.         //得到map集合的key键值的set集合  
  43.         Set setkeys=map.keySet();  
  44.         //得到key键值set集合的迭代器  
  45.         Iterator itkeys=setkeys.iterator();  
  46.         //迭代键值  
  47.         while(itkeys.hasNext()){  
  48.             //得到一个具体的键值  
  49.             Integer key = itkeys.next();  
  50.             //通过map集合的get(key)方法 获取key键值对应的value值  
  51.             User user =map.get(key);  
  52.             System.out.println(key+"------"+user.getName()+"-----"+"------"+user.getAge());  
  53.         }  
  54.           
  55.         System.out.println("--------------map2--------------");  
  56.         //map2  
  57.         //获取实体对象的set集合  
  58.         Set> setentry=map.entrySet();  
  59.         //获取实体对象的迭代器  
  60.         Iterator> itentry =setentry.iterator();  
  61.         //迭代  
  62.         while(itentry.hasNext()){  
  63.             //得到具体的Entry对象  
  64.             Entry entry=itentry.next();  
  65.             //通过entry对象的getKey()和getValue得到  
  66.             System.out.println(entry.getKey()+"----"+entry.getValue().getName()+"------"+entry.getValue().getAge());  
  67.         }  
  68.           
  69.           
  70.         System.out.println("--------------props-------------");  
  71.         Properties props = bean.props;  
  72.           
  73.         //得到这个结合键值的key的set集合  
  74.         Set setprops = props.stringPropertyNames();  
  75.         //String集合迭代器  
  76.         Iterator keystr = setprops.iterator();  
  77.           
  78.         while(keystr.hasNext()){  
  79.             //具体键值  
  80.             String key = keystr.next();  
  81.             //getProperty(key)获取key对应的value值  
  82.             System.out.println(key+"-----"+props.getProperty(key));  
  83.         }  
  84.     }  
  85. }  

控制台输出:

2013-4-25 10:09:49 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@50c4fe76: startup date [Thu Apr 25 10:09:49 CST 2013]; root of context hierarchy
2013-4-25 10:09:49 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-collection.xml]
2013-4-25 10:09:49 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3ff2cea2: defining beans [collectionBean,u1,u2,u3,u4]; root of factory hierarchy
========================
---------------set----------------
岑红军
军哥
哈哈
呵呵
嘿嘿
洗洗
--------------list----------------
deep-------------21
deepsoul-------------22
chrp-------------23
redarmy-------------28
----------------map1---------------
1------deep-----------21
2------deepsoul-----------22
3------chrp-----------23
--------------map2--------------
1----deep------21
2----deepsoul------22
3----chrp------23
--------------props-------------
3-----jdbc:access
2-----jdbc:mysql

你可能感兴趣的:(spring)