Spring 中如何为Bean注入集合呢?

转自:

Spring 中如何为Bean注入集合呢?

下文讲述Spring中为Bean注入集合的方法分享,如下所示:

常见的集合类型有:
List、Set、Map 和 properties


标签

集合名称说明
<list>用于注入 list 类型的值,允许重复
<set>用于注入 set 类型的值,不允许重复
<map>用于注入 key-value 的集合,其中 key-value 可以是任意类型
<props>用于注入 key-value 的集合,其中 key-value 都是字符串类型

例:

  1. 创建SpringDemo 项目
  2. 在src目录下创建 com.java265 包
  3. 添加相应的 jar 包,可以查看我的第一个Spring程序
  4. 在 com.java265 包下创建 JavaCollection、Man 和 MainApp 类
  5. 在 src 目录下创建 Spring 配置文件 Beans.xml
  6. 运行 SpringDemo 项目

JavaCollection 类

package com.java265;
import java.util.*;
public class JavaCollection {
    List manList;
    Set manSet;
    Map manMap;
    Properties manProp;
    public void setManList(List manList) {
        this.manList = manList;
    }
    public List getManList() {
        System.out.println("List Elements :" + manList);
        return manList;
    }
    public void setManSet(Set manSet) {
        this.manSet = manSet;
    }
    public Set getManSet() {
        System.out.println("Set Elements :" + manSet);
        return manSet;
    }
    public void setManMap(Map manMap) {
        this.manMap = manMap;
    }
    public Map getManMap() {
        System.out.println("Map Elements :" + manMap);
        return manMap;
    }
    public void setManProp(Properties manProp) {
        this.manProp = manProp;
    }
    public Properties getManProp() {
        System.out.println("Property Elements :" + manProp);
        return manProp;
    }
}

MainApp 类

package com.java265;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        JavaCollection jc = (JavaCollection) context.getBean("javaCollection");
        jc.getManList();
        jc.getManSet();
        jc.getManMap();
        jc.getManProp();
    }
}

Beans.xml 配置文件

<?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-3.0.xsd">
    <bean id="javaCollection" class="com.java265.JavaCollection">
        <property name="manList">
            <list>
                <value>java265爱好者</value>
                <value>百度</value>
                <value>java265中文站</value>
                <value>java265中文站</value>
            </list>
        </property>
        <property name="manSet">
            <set>
                <value>java265爱好者</value>
                <value>百度</value>
                <value>java265中文站</value>
                <value>java265中文站</value>
            </set>
        </property>
        <property name="manMap">
            <map>
                <entry key="1" value="java265爱好者" />
                <entry key="2" value="百度" />
                <entry key="3" value="java265中文站" />
                <entry key="4" value="java265中文站" />
            </map>
        </property>
        <property name="manProp">
            <props>
                <prop key="one">java265爱好者</prop>
                <prop key="one">java265爱好者</prop>
                <prop key="two">百度</prop>
                <prop key="three">java265中文站</prop>
                <prop key="four">java265中文站</prop>
            </props>
        </property>
    </bean>
</beans>

运行结果-----
List Elements :[java265爱好者, 百度, java265中文站, java265中文站]
Set Elements :[java265爱好者, 百度, java265中文站]
Map Elements :{1=java265爱好者, 2=百度, 3=java265中文站, 4=java265中文站}
Property Elements :{two=百度, one=java265爱好者, three=java265中文站, four=java265中文站}

注入Bean引用
也可以在集合元素中注入 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-3.0.xsd">
    <bean class="..." id="...">
        <property name="manList">
            <list>
                <ref bean="man1">
                <ref bean="man2">
                <value>java265爱好者</value>
            </ref></ref></list>
        </property>
        <property name="manSet">
            <set>
                <ref bean="man1">
                <ref bean="man2">
                <value>java265爱好者</value>
            </ref></ref></set>
        </property>
        <property name="manMap"><map>
                <entry key="one" value="java265爱好者">
                <entry key="two" value-ref="man1">
                <entry key="three" value-ref="man2">
            </entry></entry></entry></map>
        </property>
    </bean>
</beans>

注入null和空字符串的值

Spring 会把属性的空参数直接当成空字符串来处理
当我们需传入一个null值,需采用以下写法

<bean class="exampleBean" id="...">
    <property name="email" value="">
</property></bean>
等同于以下set代码
exampleBean.setEmail("")

当需放入NULL到属性值上时,此时<null>元素用于传入Null值
<bean class="exampleBean" id="...">
    <property name="email"><null></null></property>
</bean>
等同于以下set代码
exampleBean.setEmail(null)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值