Spring 集合注入 [ Collection Injection ]

来源:互联网 发布:简易电气画图软件 编辑:程序博客网 时间:2024/04/29 21:13

    对于简单数据类型(byte,char,short,int,float,double,long )或者String的注入,一般只需写入标签<value>即可。比如:

<property name="propertyName" value="simpleValue" />
或者

<property name="propertyName">    <value>simpleValue</value></property>

或者p模式


  如果需要注入的是集合(list,set,map,pros),那该怎么做呢?

如果集合的泛型是简单数据类型,比如 List<String>,可以这样实现:

<property name="students">    <list>        <value>student1</value>        <value>student2</value>        ...    </list></property>

如果集合的泛型是引用类型,比如List<Student>,可以利用内部bean实现

<property name="students">    <list>        <bean class="FullQualifiedNameClass">            <property name="name" value="zhangsan" />            <property name="age" value="20" />        </bean>        <bean class="FullQualifiedNameClass">            <property name="name" value="lisi" />            <property name="age" value="22" />        </bean>        ......    </list></property>

下面是一个具体例子:

省份类:

public class Province {private String name;  // 省份名称private long population;      // 人口private List<String> cities;  // 城市列表private List<Official> officials;  // 官员         // 标准setter 和 getter 以及 toString方法省略}

public class Official {private String name;private String title;private String age;       // 省略setter 和 getter 以及 toString方法}

bean配置文件:

<?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="sichuan" class="org.spring.collection.Province" scope="singleton">    <property name="name">        <value>四川</value>    </property>    <property name="population" value="87225224" />    <property name="cities">        <list>            <value>成都</value>            <value>绵阳</value>            <value>德阳</value>            <value>攀枝花</value>            <value>遂宁</value>            <value>江油</value>        </list>    </property>    <property name="officials">        <list>            <bean class="org.spring.collection.Official">                <property name="name" value="zhangsan" />                <property name="age" value="45" />                <property name="title" value="mayor" />            </bean>            <bean class="org.spring.collection.Official">                <property name="name" value="lisi" />                <property name="age" value="55" />                <property name="title" value="coutrier" />            </bean>            <bean class="org.spring.collection.Official">                <property name="name" value="wangwu" />                <property name="age" value="42" />                <property name="title" value="villager" />            </bean>                    </list>    </property></bean></beans>

运行它:

public class App {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"SpringConfig.xml"});Province siChuan = (Province) context.getBean("sichuan");System.out.println(siChuan);}}

打印输出:

Province [name=四川, population=87225224, cities=[成都, 绵阳, 德阳, 攀枝花, 遂宁, 江油], officials=[Official [name=zhangsan, title=mayor, age=45], Official [name=lisi, title=coutrier, age=55], Official [name=wangwu, title=villager, age=42]]]

这里只讲了List集合,Set,Map以及Properties以后再讲吧。