Spring_集合装配

转:http://blog.csdn.net/ailiandeziwei/article/details/8848199

——————————————————————————————————

案例分析:

1、创建相应的Java类

1.1创建一个CollectionBean存放Java Collections types List、Set、Map and Properties集合对象。

  1. package www.csdn.spring.collection.set;  
  2.   
  3.   
  4. import java.util.List;  
  5.   
  6. import java.util.Map;  
  7.   
  8. import java.util.Properties;  
  9.   
  10. import java.util.Set;  
  11.   
  12.   
  13. public class CollectionBean {  
  14.   
  15.   
  16. // set集合   
  17.   
  18. public Set<String> sets;  
  19.   
  20.   
  21. public void setSets(Set<String> sets) {  
  22.   
  23. this.sets = sets;  
  24.   
  25. }  
  26.   
  27.   
  28. // list集合   
  29.   
  30. public List<User> users;  
  31.   
  32.   
  33. public void setUsers(List<User> users) {  
  34.   
  35. this.users = users;  
  36.   
  37. }  
  38.   
  39.   
  40. // map集合   
  41.   
  42. public Map<Integer, User> map;  
  43.   
  44.   
  45. public void setMap(Map<Integer, User> map) {  
  46.   
  47. this.map = map;  
  48.   
  49. }  
  50.   
  51.   
  52. // props集合   
  53.   
  54. public Properties props;  
  55.   
  56.   
  57. public void setProps(Properties props) {  
  58.   
  59. this.props = props;  
  60.   
  61. }  
  62.   
  63.   
  64. }  
  1. package www.csdn.spring.collection.set;  
  2.   
  3.   
  4. import java.util.List;  
  5.   
  6. import java.util.Map;  
  7.   
  8. import java.util.Properties;  
  9.   
  10. import java.util.Set;  
  11.   
  12.   
  13. public class CollectionBean {  
  14.   
  15.   
  16. // set集合   
  17.   
  18. public Set<String> sets;  
  19.   
  20.   
  21. public void setSets(Set<String> sets) {  
  22.   
  23. this.sets = sets;  
  24.   
  25. }  
  26.   
  27.   
  28. // list集合   
  29.   
  30. public List<User> users;  
  31.   
  32.   
  33. public void setUsers(List<User> users) {  
  34.   
  35. this.users = users;  
  36.   
  37. }  
  38.   
  39.   
  40. // map集合   
  41.   
  42. public Map<Integer, User> map;  
  43.   
  44.   
  45. public void setMap(Map<Integer, User> map) {  
  46.   
  47. this.map = map;  
  48.   
  49. }  
  50.   
  51.   
  52. // props集合   
  53.   
  54. public Properties props;  
  55.   
  56.   
  57. public void setProps(Properties props) {  
  58.   
  59. this.props = props;  
  60.   
  61. }  
  62.   
  63.   
  64. }  
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的代码如下:

  1. package www.csdn.spring.collection.set;  
  2.   
  3.   
  4. public class User {  
  5.   
  6.   
  7. private String name;  
  8.   
  9. private Integer age;  
  10.   
  11.   
  12. public String getName() {  
  13.   
  14. return name;  
  15.   
  16. }  
  17.   
  18. public void setName(String name) {  
  19.   
  20. this.name = name;  
  21.   
  22. }  
  23.   
  24. public Integer getAge() {  
  25.   
  26. return age;  
  27.   
  28. }  
  29.   
  30. public void setAge(Integer age) {  
  31.   
  32. this.age = age;  
  33.   
  34. }  
  35.   
  36.   
  37.   
  38. }  
  1. package www.csdn.spring.collection.set;  
  2.   
  3.   
  4. public class User {  
  5.   
  6.   
  7. private String name;  
  8.   
  9. private Integer age;  
  10.   
  11.   
  12. public String getName() {  
  13.   
  14. return name;  
  15.   
  16. }  
  17.   
  18. public void setName(String name) {  
  19.   
  20. this.name = name;  
  21.   
  22. }  
  23.   
  24. public Integer getAge() {  
  25.   
  26. return age;  
  27.   
  28. }  
  29.   
  30. public void setAge(Integer age) {  
  31.   
  32. this.age = age;  
  33.   
  34. }  
  35.   
  36.   
  37.   
  38. }  
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

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.   
  9.            http://www.springframework.org/schema/beans/spring-beans.xsd">  
  10.   
  11.              
  12.   
  13. <bean id="collectionBean" class="www.csdn.spring.collection.set.CollectionBean"  
  14.   
  15. scope="singleton" lazy-init="default">  
  16.   
  17. <!-- set集合 -->  
  18.   
  19. <property name="sets">  
  20.   
  21. <set>  
  22.   
  23. <value>陈红军</value>  
  24.   
  25. <value>军哥</value>  
  26.   
  27. </set>  
  28.   
  29. </property>  
  30.   
  31. <!-- list集合 -->  
  32.   
  33. <property name="users">  
  34.   
  35.    <!-- 采用array配置 -->  
  36.   
  37. <array>  
  38.   
  39. <ref bean="u1" />  
  40.   
  41. <ref bean="u2" />  
  42.   
  43. </array>  
  44.   
  45. <!-- 或者采用list配置-->  
  46.   
  47. <!--    
  48.   
  49.  <list>   
  50.   
  51.      <ref bean="u1"/>  
  52.   
  53.                  <ref bean="u2"/>   
  54.   
  55.  </list>  
  56.   
  57.  -->  
  58.   
  59. </property>  
  60.   
  61. <!-- map集合 -->  
  62.   
  63. <property name="map">  
  64.   
  65. <map>  
  66.   
  67. <entry key="1" value-ref="u1" />  
  68.   
  69. <entry key="2">  
  70.   
  71. <ref bean="u2" />  
  72.   
  73. </entry>  
  74.   
  75. </map>  
  76.   
  77. </property>  
  78.   
  79. <!-- props集合 -->  
  80.   
  81. <property name="props">  
  82.   
  83. <props>  
  84.   
  85. <prop key="1">jdbc:oracle</prop>  
  86.   
  87. <prop key="2">jdbc:mysql</prop>  
  88.   
  89. <prop key="3">jdbc:access</prop>  
  90.   
  91. </props>  
  92.   
  93. </property>  
  94.   
  95. </bean>  
  96.   
  97.   
  98.     <!-- User实体Bean的配置 -->  
  99.   
  100. <bean id="u1" class="www.csdn.spring.collection.set.User">  
  101.   
  102. <property name="name" value="陈红均" />  
  103.   
  104. <property name="age" value="28" />  
  105.   
  106. </bean>  
  107.   
  108. <bean id="u2" class="www.csdn.spring.collection.set.User">  
  109.   
  110. <property name="name" value="信心套" />  
  111.   
  112. <property name="age" value="28" />  
  113.   
  114. </bean>  
  115.   
  116. </beans>  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.   
  9.            http://www.springframework.org/schema/beans/spring-beans.xsd">  
  10.   
  11.              
  12.   
  13. <bean id="collectionBean" class="www.csdn.spring.collection.set.CollectionBean"  
  14.   
  15. scope="singleton" lazy-init="default">  
  16.   
  17. <!-- set集合 -->  
  18.   
  19. <property name="sets">  
  20.   
  21. <set>  
  22.   
  23. <value>陈红军</value>  
  24.   
  25. <value>军哥</value>  
  26.   
  27. </set>  
  28.   
  29. </property>  
  30.   
  31. <!-- list集合 -->  
  32.   
  33. <property name="users">  
  34.   
  35.    <!-- 采用array配置 -->  
  36.   
  37. <array>  
  38.   
  39. <ref bean="u1" />  
  40.   
  41. <ref bean="u2" />  
  42.   
  43. </array>  
  44.   
  45. <!-- 或者采用list配置-->  
  46.   
  47. <!--    
  48.   
  49.  <list>   
  50.   
  51.      <ref bean="u1"/>  
  52.   
  53.                  <ref bean="u2"/>   
  54.   
  55.  </list>  
  56.   
  57.  -->  
  58.   
  59. </property>  
  60.   
  61. <!-- map集合 -->  
  62.   
  63. <property name="map">  
  64.   
  65. <map>  
  66.   
  67. <entry key="1" value-ref="u1" />  
  68.   
  69. <entry key="2">  
  70.   
  71. <ref bean="u2" />  
  72.   
  73. </entry>  
  74.   
  75. </map>  
  76.   
  77. </property>  
  78.   
  79. <!-- props集合 -->  
  80.   
  81. <property name="props">  
  82.   
  83. <props>  
  84.   
  85. <prop key="1">jdbc:oracle</prop>  
  86.   
  87. <prop key="2">jdbc:mysql</prop>  
  88.   
  89. <prop key="3">jdbc:access</prop>  
  90.   
  91. </props>  
  92.   
  93. </property>  
  94.   
  95. </bean>  
  96.   
  97.   
  98.     <!-- User实体Bean的配置 -->  
  99.   
  100. <bean id="u1" class="www.csdn.spring.collection.set.User">  
  101.   
  102. <property name="name" value="陈红均" />  
  103.   
  104. <property name="age" value="28" />  
  105.   
  106. </bean>  
  107.   
  108. <bean id="u2" class="www.csdn.spring.collection.set.User">  
  109.   
  110. <property name="name" value="信心套" />  
  111.   
  112. <property name="age" value="28" />  
  113.   
  114. </bean>  
  115.   
  116. </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="陈红均" />

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

  1. @Test  
  2.   
  3. public void testSets() {  
  4.   
  5. //创建应用上下文对象   
  6.   
  7. ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-collection.xml");  
  8.   
  9. //根据上下文对象的getBean方法获取指定的Bean对象   
  10.   
  11. CollectionBean bean = context.getBean("collectionBean",CollectionBean.class);  
  12.   
  13.   
  14. System.out.println("-----------------------set------------------------------");  
  15.   
  16. // 获取set集合   
  17.   
  18. Set<String> sets = bean.sets;  
  19.   
  20. // 得到迭代器   
  21.   
  22. Iterator<String> it = sets.iterator();  
  23.   
  24.   
  25. while (it.hasNext()) {  
  26.   
  27. System.out.println(it.next());  
  28.   
  29. }  
  30.   
  31.   
  32. System.out.println("-----------------------list------------------------------");  
  33.   
  34. List<User> users = bean.users;  
  35.   
  36. for (User u : users) {  
  37.   
  38. System.out.println(u.getName() + "-------------" + u.getAge());  
  39.   
  40. }  
  41.   
  42.   
  43. //map第一种遍历方式   
  44.   
  45. System.out.println("-----------------------map1------------------------------");  
  46.   
  47.   
  48. Map<Integer, User> map = bean.map;  
  49.   
  50. // 得到map集合的key键值的set集合   
  51.   
  52. Set<Integer> setkeys = map.keySet();  
  53.   
  54. // 得到key键值set集合的迭代器   
  55.   
  56. Iterator<Integer> itkeys = setkeys.iterator();  
  57.   
  58. // 迭代键值   
  59.   
  60. while (itkeys.hasNext()) {  
  61.   
  62. // 得到一个具体的键值   
  63.   
  64. Integer key = itkeys.next();  
  65.   
  66. // 通过map集合的get(key)方法 获取key键值对应的value值   
  67.   
  68. User user = map.get(key);  
  69.   
  70. System.out.println(key + "===========" + user.getName()+ "======" + user.getAge());  
  71.   
  72. }  
  73.   
  74.   
  75. // map第二种遍历方式   
  76.   
  77. System.out.println("----------------------------map2-----------------------------------");  
  78.   
  79.   
  80. // 获取实体对象的set集合   
  81.   
  82. Set<Entry<Integer, User>> setentry = map.entrySet();  
  83.   
  84. // 获取实体对象的迭代器   
  85.   
  86. Iterator<Entry<Integer, User>> itentry = setentry.iterator();  
  87.   
  88. // 迭代   
  89.   
  90. while (itentry.hasNext()) {  
  91.   
  92. // 得到具体的Entry对象   
  93.   
  94. Entry<Integer, User> entry = itentry.next();  
  95.   
  96. // 通过entry对象的getKey() 和getValue分别得到key与value值   
  97.   
  98. System.out.println(entry.getKey() + "======="+entry.getValue().getName() + "====="+ entry.getValue().getAge());  
  99.   
  100.   
  101. }  
  102.   
  103.   
  104.   
  105.   
  106. System.out.println("--------------------------props---------------------------");  
  107.   
  108.   
  109. Properties props = bean.props;  
  110.   
  111.   
  112. //得到这个集合键值的key的set集合   
  113.   
  114. Set<String> setprops = props.stringPropertyNames();  
  115.   
  116. //key集合迭代器   
  117.   
  118. Iterator<String> keystr = setprops.iterator();  
  119.   
  120.   
  121. while(keystr.hasNext()){  
  122.   
  123. //具体键值   
  124.   
  125. String key = keystr.next();  
  126.   
  127. //getProperty(key)获取key对应的value值   
  128.   
  129. System.out.println(key+"-------------"+props.getProperty(key));  
  130.   
  131. }  
  132.   
  133.   
  134.   
  135. }  
  1. @Test  
  2.   
  3. public void testSets() {  
  4.   
  5. //创建应用上下文对象   
  6.   
  7. ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-collection.xml");  
  8.   
  9. //根据上下文对象的getBean方法获取指定的Bean对象   
  10.   
  11. CollectionBean bean = context.getBean("collectionBean",CollectionBean.class);  
  12.   
  13.   
  14. System.out.println("-----------------------set------------------------------");  
  15.   
  16. // 获取set集合   
  17.   
  18. Set<String> sets = bean.sets;  
  19.   
  20. // 得到迭代器   
  21.   
  22. Iterator<String> it = sets.iterator();  
  23.   
  24.   
  25. while (it.hasNext()) {  
  26.   
  27. System.out.println(it.next());  
  28.   
  29. }  
  30.   
  31.   
  32. System.out.println("-----------------------list------------------------------");  
  33.   
  34. List<User> users = bean.users;  
  35.   
  36. for (User u : users) {  
  37.   
  38. System.out.println(u.getName() + "-------------" + u.getAge());  
  39.   
  40. }  
  41.   
  42.   
  43. //map第一种遍历方式   
  44.   
  45. System.out.println("-----------------------map1------------------------------");  
  46.   
  47.   
  48. Map<Integer, User> map = bean.map;  
  49.   
  50. // 得到map集合的key键值的set集合   
  51.   
  52. Set<Integer> setkeys = map.keySet();  
  53.   
  54. // 得到key键值set集合的迭代器   
  55.   
  56. Iterator<Integer> itkeys = setkeys.iterator();  
  57.   
  58. // 迭代键值   
  59.   
  60. while (itkeys.hasNext()) {  
  61.   
  62. // 得到一个具体的键值   
  63.   
  64. Integer key = itkeys.next();  
  65.   
  66. // 通过map集合的get(key)方法 获取key键值对应的value值   
  67.   
  68. User user = map.get(key);  
  69.   
  70. System.out.println(key + "===========" + user.getName()+ "======" + user.getAge());  
  71.   
  72. }  
  73.   
  74.   
  75. // map第二种遍历方式   
  76.   
  77. System.out.println("----------------------------map2-----------------------------------");  
  78.   
  79.   
  80. // 获取实体对象的set集合   
  81.   
  82. Set<Entry<Integer, User>> setentry = map.entrySet();  
  83.   
  84. // 获取实体对象的迭代器   
  85.   
  86. Iterator<Entry<Integer, User>> itentry = setentry.iterator();  
  87.   
  88. // 迭代   
  89.   
  90. while (itentry.hasNext()) {  
  91.   
  92. // 得到具体的Entry对象   
  93.   
  94. Entry<Integer, User> entry = itentry.next();  
  95.   
  96. // 通过entry对象的getKey() 和getValue分别得到key与value值   
  97.   
  98. System.out.println(entry.getKey() + "======="+entry.getValue().getName() + "====="+ entry.getValue().getAge());  
  99.   
  100.   
  101. }  
  102.   
  103.   
  104.   
  105.   
  106. System.out.println("--------------------------props---------------------------");  
  107.   
  108.   
  109. Properties props = bean.props;  
  110.   
  111.   
  112. //得到这个集合键值的key的set集合   
  113.   
  114. Set<String> setprops = props.stringPropertyNames();  
  115.   
  116. //key集合迭代器   
  117.   
  118. Iterator<String> keystr = setprops.iterator();  
  119.   
  120.   
  121. while(keystr.hasNext()){  
  122.   
  123. //具体键值   
  124.   
  125. String key = keystr.next();  
  126.   
  127. //getProperty(key)获取key对应的value值   
  128.   
  129. System.out.println(key+"-------------"+props.getProperty(key));  
  130.   
  131. }  
  132.   
  133.   
  134.   
  135. }  
原文地址:https://www.cnblogs.com/kaikailele/p/3960365.html