标签:style blog http io 使用 java ar strong for
转:http://blog.csdn.net/ailiandeziwei/article/details/8848199
——————————————————————————————————
案例分析:
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、创建测试类 测试代码如下:
标签:style blog http io 使用 java ar strong for
原文地址:http://www.cnblogs.com/kaikailele/p/3960365.html