首页 > 代码库 > Spring_集合装配
Spring_集合装配
转:http://blog.csdn.net/ailiandeziwei/article/details/8848199
——————————————————————————————————
案例分析:
1、创建相应的Java类
1.1创建一个CollectionBean存放Java Collections types List、Set、Map and Properties集合对象。
[java] view plaincopyprint?
- 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;
- }
- }
[java] view plaincopyprint?
- 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;
- }
- }
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的代码如下:
[java] view plaincopyprint?
- 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;
- }
- }
[java] view plaincopyprint?
- 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;
- }
- }
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
[html] view plaincopyprint?
- <?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=http://www.mamicode.com/"陈红均" />
- <property name="age" value=http://www.mamicode.com/"28" />
- </bean>
- <bean id="u2" class="www.csdn.spring.collection.set.User">
- <property name="name" value=http://www.mamicode.com/"信心套" />
- <property name="age" value=http://www.mamicode.com/"28" />
- </bean>
- </beans>
[html] view plaincopyprint?
- <?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=http://www.mamicode.com/"陈红均" />
- <property name="age" value=http://www.mamicode.com/"28" />
- </bean>
- <bean id="u2" class="www.csdn.spring.collection.set.User">
- <property name="name" value=http://www.mamicode.com/"信心套" />
- <property name="age" value=http://www.mamicode.com/"28" />
- </bean>
- </beans>
<?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="http://www.mamicode.com/陈红均" /><property name="age" value="http://www.mamicode.com/28" /></bean><bean id="u2" class="www.csdn.spring.collection.set.User"><property name="name" value="http://www.mamicode.com/信心套" /><property name="age" value="http://www.mamicode.com/28" /></bean></beans>
3、创建测试类 测试代码如下:
[java] view plaincopyprint?
- @Test
- public 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));
- }
- }
[java] view plaincopyprint?
- @Test
- public 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));
- }
- }
Spring_集合装配
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。