Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 312|回复: 0

[默认分类] Spring中注入List,Set,Map,Properties

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-6-1 12:34:56 | 显示全部楼层 |阅读模式
    下面的例子展示了如何注入

    List – <list/>
    Set – <set/>
    Map – <map/>
    Properties – <props/>

    Spring beans

    1. import java.util.List;
    2. import java.util.Map;
    3. import java.util.Properties;
    4. import java.util.Set;
    5. public class Customer
    6. {
    7.         private List<Object> lists;
    8.         private Set<Object> sets;
    9.         private Map<Object, Object> maps;
    10.         private Properties pros;
    11.         //...
    12. }
    复制代码


      配置文件:

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3.         xsi:schemaLocation="http://www.springframework.org/schema/beans
    4.         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    5.         <bean id="CustomerBean" class="com.mkyong.common.Customer">
    6.                 <!-- java.util.List -->
    7.                 <property name="lists">
    8.                         <list>
    9.                                 <value>1</value>
    10.                                 <ref bean="PersonBean" />
    11.                                 <bean class="com.mkyong.common.Person">
    12.                                         <property name="name" value="mkyongList" />
    13.                                         <property name="address" value="address" />
    14.                                         <property name="age" value="28" />
    15.                                 </bean>
    16.                         </list>
    17.                 </property>
    18.                 <!-- java.util.Set -->
    19.                 <property name="sets">
    20.                         <set>
    21.                                 <value>1</value>
    22.                                 <ref bean="PersonBean" />
    23.                                 <bean class="com.mkyong.common.Person">
    24.                                         <property name="name" value="mkyongSet" />
    25.                                         <property name="address" value="address" />
    26.                                         <property name="age" value="28" />
    27.                                 </bean>
    28.                         </set>
    29.                 </property>
    30.                 <!-- java.util.Map -->
    31.                 <property name="maps">
    32.                         <map>
    33.                                 <entry key="Key 1" value="1" />
    34.                                 <entry key="Key 2" value-ref="PersonBean" />
    35.                                 <entry key="Key 3">
    36.                                         <bean class="com.mkyong.common.Person">
    37.                                                 <property name="name" value="mkyongMap" />
    38.                                                 <property name="address" value="address" />
    39.                                                 <property name="age" value="28" />
    40.                                         </bean>
    41.                                 </entry>
    42.                         </map>
    43.                 </property>
    44.                 <!-- java.util.Properties -->
    45.                 <property name="pros">
    46.                         <props>
    47.                                 <prop key="admin">admin@nospam.com</prop>
    48.                                 <prop key="support">support@nospam.com</prop>
    49.                         </props>
    50.                 </property>
    51.         </bean>
    52.         <bean id="PersonBean" class="com.mkyong.common.Person">
    53.                 <property name="name" value="mkyong1" />
    54.                 <property name="address" value="address 1" />
    55.                 <property name="age" value="28" />
    56.         </bean>
    57. </beans>
    复制代码


    运行:

    1. public class App
    2. {
    3.     public static void main( String[] args )
    4.     {
    5.             ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
    6.             Customer cust = (Customer)context.getBean("CustomerBean");
    7.             System.out.println(cust);
    8.     }
    9. }
    复制代码


      我们也可以使用ListFactoryBean。The ‘ListFactoryBean‘ class provides developer a way to create a concrete List collection class (ArrayList and LinkedList) in Spring’s bean configuration file.

    1. <bean id="CustomerBean" class="com.mkyong.common.Customer">
    2.                 <property name="lists">
    3.                         <bean class="org.springframework.beans.factory.config.ListFactoryBean">
    4.                                 <property name="targetListClass">
    5.                                         <value>java.util.ArrayList</value>
    6.                                 </property>
    7.                                 <property name="sourceList">
    8.                                         <list>
    9.                                                 <value>1</value>
    10.                                                 <value>2</value>
    11.                                                 <value>3</value>
    12.                                         </list>
    13.                                 </property>
    14.                         </bean>
    15.                 </property>
    16.         </bean>
    复制代码


      或者:加入:xmlns:util="http://www.springframework.org/schema/util"然后就可以:

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3.         xmlns:util="http://www.springframework.org/schema/util"
    4.         xsi:schemaLocation="http://www.springframework.org/schema/beans
    5.         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    6.         http://www.springframework.org/schema/util
    7.         http://www.springframework.org/schema/util/spring-util-2.5.xsd">
    8.         <bean id="CustomerBean" class="com.mkyong.common.Customer">
    9.                 <property name="lists">
    10.                         <util:list list-class="java.util.ArrayList">
    11.                                 <value>1</value>
    12.                                 <value>2</value>
    13.                                 <value>3</value>
    14.                         </util:list>
    15.                 </property>
    16.         </bean>
    17. </beans>
    复制代码


      
             SetFactoryBean The ‘SetFactoryBean‘ class provides developer a way to create a concrete Set collection (HashSet and TreeSet) in Spring’s bean configuration file.
    set和上面的一样:

    1. <bean id="CustomerBean" class="com.mkyong.common.Customer">
    2.                 <property name="sets">
    3.                         <util:set set-class="java.util.HashSet">
    4.                                 <value>1</value>
    5.                                 <value>2</value>
    6.                                 <value>3</value>
    7.                         </util:set>
    8.                 </property>
    9.         </bean>
    复制代码


             MapFactoryBean The ‘MapFactoryBean‘ class provides developer a way to create a concrete Map collection class (HashMap and TreeMap) in Spring’s bean configuration file.
    map也一样:

    1. <bean id="CustomerBean" class="com.mkyong.common.Customer">
    2.                 <property name="maps">
    3.                         <util:map map-class="java.util.HashMap">
    4.                                 <entry key="Key1" value="1" />
    5.                                 <entry key="Key2" value="2" />
    6.                                 <entry key="Key3" value="3" />
    7.                         </util:map>
    8.                 </property>
    9.         </bean>
    复制代码


      

      
      
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-24 11:20 , Processed in 0.401709 second(s), 37 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表