zoukankan      html  css  js  c++  java
  • 【Spring学习笔记-4】注入集合类List、Set、Map、Pros等

    概要:
    当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企业应用实战]





  • 相关阅读:
    总有段迷惑的人生
    codepage的重要性【转】
    开通博客
    js 正则常用方法
    关于小程序scrollview组件添加enableflex后布局失效的解决方案
    关于小程序使用async/await报错regeneratorRuntime is not defined的解决方案
    IE6中,一个Button同时打开两个下载窗口,并且可以自动关闭
    Create User
    Oracle: import tables use .dmp file in PL/SQL Developer
    VS在进行调试时,不能调试的原因列举如下
  • 原文地址:https://www.cnblogs.com/ssslinppp/p/4380147.html
Copyright © 2011-2022 走看看