Spring_集合装配

来源:互联网 发布:颜宁对韩春雨评价 知乎 编辑:程序博客网 时间:2024/05/22 17:46

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 ListSetMap 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、创建测试类 测试代码如下:

@Testpublic 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、执行结果

原创粉丝点击