ssslinppp

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
概要:
当java类中含有集合属性:如List、Set、Map、Pros等时,Spring配置文件中该如何配置呢?
下面将进行讲解。

整体结构:

接口
Axe.java 

  1. package org.crazyit.app.service;
  2. public interface Axe
  3. {
  4. public String chop();
  5. }

Person.java

  1. package org.crazyit.app.service;
  2. public interface Person
  3. {
  4. public void test();
  5. }



实现类
Chinese.java

  1. package org.crazyit.app.service.impl;
  2. import java.util.*;
  3. import org.crazyit.app.service.*;
  4. public class Chinese implements Person
  5. {
  6. // 下面是系列集合类型的成员变量
  7. private List<String> schools;
  8. private Map scores;
  9. private Map<String , Axe> phaseAxes;
  10. private Properties health;
  11. private Set axes;
  12. private String[] books;
  13. public Chinese()
  14. {
  15. System.out.println("Spring实例化主调bean:Chinese实例...");
  16. }
  17. // schools的setter方法
  18. public void setSchools(List schools)
  19. {
  20. this.schools = schools;
  21. }
  22. // scores的setter方法
  23. public void setScores(Map scores)
  24. {
  25. this.scores = scores;
  26. }
  27. // phaseAxes的setter方法
  28. public void setPhaseAxes(Map<String , Axe> phaseAxes)
  29. {
  30. this.phaseAxes = phaseAxes;
  31. }
  32. // health的setter方法
  33. public void setHealth(Properties health)
  34. {
  35. this.health = health;
  36. }
  37. // axes的setter方法
  38. public void setAxes(Set axes)
  39. {
  40. this.axes = axes;
  41. }
  42. // books的setter方法
  43. public void setBooks(String[] books)
  44. {
  45. this.books = books;
  46. }
  47. // 访问上面全部的集合类型的成员变量
  48. public void test()
  49. {
  50. System.out.println(schools);
  51. System.out.println(scores);
  52. System.out.println(phaseAxes);
  53. System.out.println(health);
  54. System.out.println(axes);
  55. System.out.println(java.util.Arrays.toString(books));
  56. }
  57. }

SteelAxe.java

  1. package org.crazyit.app.service.impl;
  2. import org.crazyit.app.service.*;
  3. public class SteelAxe implements Axe
  4. {
  5. public String chop()
  6. {
  7. return "钢斧砍柴真快";
  8. }
  9. }


StoneAxe.java

  1. package org.crazyit.app.service.impl;
  2. import org.crazyit.app.service.*;
  3. public class StoneAxe implements Axe
  4. {
  5. public String chop()
  6. {
  7. return "石斧砍柴好慢";
  8. }
  9. }

配置文件:
beans.xml
  1. <?xml version="1.0" encoding="GBK"?>
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
  6. <!-- 定义2个普通Axe Bean -->
  7. <bean id="stoneAxe" class="org.crazyit.app.service.impl.StoneAxe"/>
  8. <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/>
  9. <!-- 定义chinese Bean -->
  10. <bean id="chinese" class="org.crazyit.app.service.impl.Chinese">
  11. <property name="schools">
  12. <!-- 为调用setSchools()方法配置List集合作为参数值 -->
  13. <list>
  14. <!-- 每个value、ref、bean...都配置一个List元素 -->
  15. <value>小学</value>
  16. <value>中学</value>
  17. <value>大学</value>
  18. </list>
  19. </property>
  20. <property name="scores">
  21. <!-- 为调用setScores()方法配置Map集合作为参数值 -->
  22. <map>
  23. <!-- 每个entry配置一个key-value对 -->
  24. <entry key="数学" value="87"/>
  25. <entry key="英语" value="89"/>
  26. <entry key="语文" value="82"/>
  27. </map>
  28. </property>
  29. <property name="phaseAxes">
  30. <!-- 为调用setPhaseAxes()方法配置Map集合作为参数值 -->
  31. <map>
  32. <!-- 每个entry配置一个key-value对 -->
  33. <entry key="原始社会" value-ref="stoneAxe"/>
  34. <entry key="农业社会" value-ref="steelAxe"/>
  35. </map>
  36. </property>
  37. <property name="health">
  38. <!-- 为调用setHealth()方法配置Properties集合作为参数值 -->
  39. <props>
  40. <!-- 每个prop元素配置一个属性项,其中key指定属性名 -->
  41. <prop key="血压">正常</prop>
  42. <prop key="身高">175</prop>
  43. </props>
  44. <!--
  45. <value>
  46. pressure=normal
  47. height=175
  48. </value> -->
  49. </property>
  50. <property name="axes">
  51. <!-- 为调用setAxes()方法配置Set集合作为参数值 -->
  52. <set>
  53. <!-- 每个value、ref、bean..都配置一个Set元素 -->
  54. <value>普通的字符串</value>
  55. <bean class="org.crazyit.app.service.impl.SteelAxe"/>
  56. <ref bean="stoneAxe"/>
  57. <!-- 为Set集合配置一个List集合作为元素 -->
  58. <list>
  59. <value>20</value>
  60. <!-- 再次为List集合配置一个Set集合作为元素 -->
  61. <set>
  62. <value type="int">30</value>
  63. </set>
  64. </list>
  65. </set>
  66. </property>
  67. <property name="books">
  68. <!-- 为调用setBooks()方法配置数组作为参数值 -->
  69. <list>
  70. <!-- 每个value、ref、bean...都配置一个数组元素 -->
  71. <value>疯狂Java讲义</value>
  72. <value>疯狂Android讲义</value>
  73. <value>轻量级Java EE企业应用实战</value>
  74. </list>
  75. </property>
  76. </bean>
  77. </beans>

测试文件:
BeanTest.java

  1. package lee;
  2. import org.springframework.context.*;
  3. import org.springframework.context.support.*;
  4. import org.crazyit.app.service.*;
  5. public class BeanTest
  6. {
  7. public static void main(String[] args)throws Exception
  8. {
  9. ApplicationContext ctx = new
  10. ClassPathXmlApplicationContext("beans.xml");
  11. // 获取容器中Bean,并调用方法。
  12. Person p = ctx.getBean("chinese" , Person.class);
  13. p.test();
  14. }
  15. }

运行结果:


Spring实例化主调bean:Chinese实例...
[小学, 中学, 大学]
{数学=87, 英语=89, 语文=82}
{原始社会=org.crazyit.app.service.impl.StoneAxe@24c414, 农业社会=org.crazyit.app.service.impl.SteelAxe@bfd10a}
{血压=正常, 身高=175}
[普通的字符串, org.crazyit.app.service.impl.SteelAxe@6b62d1, org.crazyit.app.service.impl.StoneAxe@24c414, [20, [30]]]
[疯狂Java讲义, 疯狂Android讲义, 轻量级Java EE企业应用实战]





posted on 2015-03-31 10:30  ssslinppp  阅读(442)  评论(0编辑  收藏  举报