11级_Java_曹建波 04.25 Spring便历Collection集合
2013-04-25 10:05
417 查看
示例
User.java
CollectionBean.java
TestBean.java
spring-collection.xml
User.java
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; } }
CollectionBean.java
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 { public Set<String> sets; public void setSets(Set<String> sets) { this.sets = sets; } public CollectionBean() { super(); System.out.println("================================="); } public List<User> users; public void setUsers(List<User> users) { this.users = users; } public Map<Integer,User> map; public void setMap(Map<Integer, User> map) { this.map = map; } public Properties props; public void setProps(Properties props) { this.props = props; } }
TestBean.java
package www.csdn.spring.collection.set; 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-collection.xml"); CollectionBean bean=context.getBean("collectionBean",CollectionBean.class); 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()); } System.out.println("----------------------map--------------"); Map<Integer, User> map=bean.map; Set<Integer> setKeys=map.keySet(); Iterator<Integer> itKeys=setKeys.iterator(); while(itKeys.hasNext()){ Integer key=itKeys.next(); User user=map.get(key); System.out.println(key+":--"+user.getName()+"--"+user.getAge()); } System.out.println("----------------------map2--------------"); 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()+"----"+entry.getValue().getName()+"----"+entry.getValue().getAge()); } System.out.println("----------------------props--------------"); Properties props=bean.props; Set<String> setprops=props.stringPropertyNames(); Iterator<String> keystr=setprops.iterator(); while(keystr.hasNext()){ String key=keystr.next(); System.out.println(key+"----"+props.getProperty(key)); } } }
spring-collection.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.csdn.spring.collection.set.CollectionBean" scope="singleton" lazy-init="default"> <property name="sets"> <set> <value>哈哈哈</value> <value>嘿嘿嘿</value> <value>呵呵呵</value> <value>ASDA</value> <value>哈啊啊是是哈</value> <value>哈啊三哈</value> </set> </property> <property name="users"> <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:mysql</prop> <prop key="2">jdbc:oracle</prop> <prop key="3">jdbc:access</prop> </props> </property> </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> <bean id="u3" class="www.csdn.spring.collection.set.User"> <property name="name" value="王五" /> <property name="age" value="30" /> </bean> <bean id="u4" class="www.csdn.spring.collection.set.User"> <property name="name" value="马六" /> <property name="age" value="28" /> </bean> </beans>
相关文章推荐
- 11级_Java_曹建波 04.23 初步认识Spring,为什么要使用Spring及优势
- 11级_Java_曹建波 04.24 Spring的配置、生命周期、依赖注入bean的操作
- JAVA SE篇--04 Collection框架及List集合操作
- Java基础知识强化之集合框架笔记04:Collection集合的基本功能测试
- 11级_Java_曹建波4.25 Java中的集合类
- 11级_Java_曹建波 05.06 spring aop术语概述
- 11级_Java_曹建波 05.06 Spring的autowire自动装配bean的四种方式
- 11级_Java_曹建波4.26 集及其实现类&枚举器与数据操作
- 黑马程序员—javaCollection集合框架
- Java基础之集合框架(一)--Collection、List、LinkedList、HashSet
- 11级_Java_曹建波 02.26 Struts2_HelloWorld
- Java基础(集合卷)--单列集合老大Collection
- Java集合Collection
- Java--面向对象之七:Collection集合框架
- java之集合Collection 详解之4
- Java.集合类.Collection.ArrayList.Iterator
- Java集合系列(一)—— Collection入门基础概括
- Java中的集合Collection以及对应的子类list类
- 11级_Java_曹建波5.30 WHERE子句
- Java 集合框架系列---Collection