【Java】Spring中注入List,Set,Map,Properties

栏目: Java · 发布时间: 6年前

内容简介:【Java】Spring中注入List,Set,Map,Properties
  • List – <list/>
  • Set – <set/>
  • Map – <map/>
  • Properties – <props/>

Spring beans

import java.util.List;


import java.util.Map;


import java.util.Properties;


import java.util.Set;


  

public class Customer

{

    private List<Object> lists;


    private Set<Object> sets;


    private Map<Object, Object> maps;


    private Properties pros;


  

    //...

}

配置文件:

<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-2.5.xsd">


  

    <bean id="CustomerBean" class="com.mkyong.common.Customer">


  

        <!-- java.util.List -->


        <property name="lists">


            <list>


                <value>1</value>


                <ref bean="PersonBean" />


                <bean class="com.mkyong.common.Person">


                    <property name="name" value="mkyongList" />


                    <property name="address" value="address" />


                    <property name="age" value="28" />


                </bean>


            </list>


        </property>


  

        <!-- java.util.Set -->


        <property name="sets">


            <set>


                <value>1</value>


                <ref bean="PersonBean" />


                <bean class="com.mkyong.common.Person">


                    <property name="name" value="mkyongSet" />


                    <property name="address" value="address" />


                    <property name="age" value="28" />


                </bean>


            </set>


        </property>


  

        <!-- java.util.Map -->


        <property name="maps">


            <map>


                <entry key="Key 1" value="1" />


                <entry key="Key 2" value-ref="PersonBean" />


                <entry key="Key 3">


                    <bean class="com.mkyong.common.Person">


                        <property name="name" value="mkyongMap" />


                        <property name="address" value="address" />


                        <property name="age" value="28" />


                    </bean>


                </entry>


            </map>


        </property>


  

        <!-- java.util.Properties -->


        <property name="pros">


            <props>


                <prop key="admin">admin@nospam.com</prop>


                <prop key="support">support@nospam.com</prop>


            </props>


        </property>


  

    </bean>


  

    <bean id="PersonBean" class="com.mkyong.common.Person">


        <property name="name" value="mkyong1" />


        <property name="address" value="address 1" />


        <property name="age" value="28" />


    </bean>


  

</beans>

运行:

public class App

{

    public static void main( String[] args )


    {


        ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");


  

        Customer cust = (Customer)context.getBean("CustomerBean");


        System.out.println(cust);


  

    }

}

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

<bean id="CustomerBean" class="com.mkyong.common.Customer">


        <property name="lists">


            <bean class="org.springframework.beans.factory.config.ListFactoryBean">


                <property name="targetListClass">


                    <value>java.util.ArrayList</value>


                </property>


                <property name="sourceList">


                    <list>


                        <value>1</value>


                        <value>2</value>


                        <value>3</value>


                    </list>


                </property>


            </bean>


        </property>


    </bean>

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

<beans xmlns="http://www.springframework.org/schema/beans"


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


    xmlns:util="http://www.springframework.org/schema/util"


    xsi:schemaLocation="http://www.springframework.org/schema/beans


    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd


    http://www.springframework.org/schema/util


    http://www.springframework.org/schema/util/spring-util-2.5.xsd">


  

    <bean id="CustomerBean" class="com.mkyong.common.Customer">


        <property name="lists">


            <util:list list-class="java.util.ArrayList">


                <value>1</value>


                <value>2</value>


                <value>3</value>


            </util:list>


        </property>


    </bean>


  

</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和上面的一样:

<bean id="CustomerBean" class="com.mkyong.common.Customer">


        <property name="sets">


            <util:set set-class="java.util.HashSet">


                <value>1</value>


                <value>2</value>


                <value>3</value>


            </util:set>


        </property>


    </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也一样:

<bean id="CustomerBean" class="com.mkyong.common.Customer">


        <property name="maps">


            <util:map map-class="java.util.HashMap">


                <entry key="Key1" value="1" />


                <entry key="Key2" value="2" />


                <entry key="Key3" value="3" />


            </util:map>


        </property>


    </bean>


以上所述就是小编给大家介绍的《【Java】Spring中注入List,Set,Map,Properties》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

ACM/ICPC程序设计与分析

ACM/ICPC程序设计与分析

沈云付 / 清华大学 / 2010-7 / 39.50元

《ACM/ICPC程序设计与分析(C++实现)》介绍ACM国际大学生程序设计竞赛概况及程序设计基础,系统介绍数论、组合数学、动态规划、计算几何、搜索、图论和网络流等专题的典型算法,挑选历年竞赛中许多有代表性的竞赛题作为例题进行分析,便于学生编程时模仿学习。每章的例题和习题都配有输入输出样例,方便学生在编程时测试与调试程序。《ACM/ICPC程序设计与分析(C++实现)》以C++为程序设计语言,以提......一起来看看 《ACM/ICPC程序设计与分析》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具