spring应用手册-IOC(XML配置实现)-(28)-注入map集合

本文详细介绍了如何在Spring应用中通过XML配置文件对Map集合进行注入,包括简单类型和引用类型的注入。通过创建Account和Group类,展示了如何在Group类中使用两个Map集合,并在配置文件中利用`<map>`和`<entry>`标签进行实例化和注入。同时,提到了构造方法注入以及在注入时同时实例化bean的方法。
摘要由CSDN通过智能技术生成

戴着假发的程序员 出品

注入map集合

spring应用手册(第一部分)


map集合和list、set的不同是,map是双列集合。所以注入时稍有不同,但是大致一样。

要使用标签 map 和 entry。 我们看案例:

准备Account类。拥有属性account,有参和无参构造方法。

/**
 * @author 戴着假发的程序员
 *  
 * @description 账户
 */
public class Account {
    private String account;
    public Account(){
    }
    public Account(String account) {
        this.account = account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    @Override
    public String toString(){
        return "account:"+account;
    }
}

准备group类,拥有两个map集合,其中一个存储账户名字(String)类型,以账号(Integer)作为key。另一个存储Account对象,以账号(Integer)作为key。并且提供有参数构造和无参数构造。

/**
 * @author 戴着假发的程序员
 *  
 * @description
 */
public class Group {
    //账户名字集合
    private Map<Integer,String> accNames;
    //账户对象集合
    private Map<Integer,Account> accounts;

    public void setAccNames(Map<Integer,String> accNames) {
        this.accNames = accNames;
    }
    public void setAccounts(Map<Integer,Account> accounts) {
        this.accounts = accounts;
    }
    //无参数构造
    public Group(){
    }
    //有参数构造
    public Group(Map<Integer,String> accNames, Map<Integer,Account> accounts) {
        this.accNames = accNames;
        this.accounts = accounts;
    }
    public void showNames(){
        System.out.println(accNames);
    }
    public void showAccount(){
        System.out.println(accounts);
    }
}

在配置文件中进行注入:

<?xml version="1.0" encoding="UTF-8"?>
<beans  default-autowire="byName" 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">
    <!-- 准备三个accountBean -->
    <bean id="account1" class="com.boxuewa.dk.demo2.beans.Account">
        <constructor-arg name="account" value="卡卡西"/>
    </bean>
    <bean id="account2" class="com.boxuewa.dk.demo2.beans.Account">
        <constructor-arg name="account" value="佐助"/>
    </bean>
    <bean id="account3" class="com.boxuewa.dk.demo2.beans.Account">
        <constructor-arg name="account" value="鸣人"/>
    </bean>
    <!-- 注册一个group -->
    <bean id="group" class="com.boxuewa.dk.demo2.beans.Group">
        <property name="accNames">
            <!-- 使用set标签注入简单set集合属性 -->
            <map>
                <entry key="9527" value="路飞"/>
                <entry key="9528" value="佐助"/>
                <entry key="9529"><value>鸣人 </value></entry>
            </map>
        </property>
        <property name="accounts">
            <!-- set标签注入引用类型属性 -->
            <map>
                <entry key="110"> <ref bean="account1"/></entry>
                <entry key="111"> <ref bean="account2"/></entry>
                <entry key="112" value-ref="account3"/>
            </map>
        </property>
    </bean>
</beans>

注意几个问题:

1、上面的注入方式同样适合构造方法的注入:

<!-- 注册一个group -->
<bean id="group" class="com.boxuewa.dk.demo2.beans.Group">
    <constructor-arg name="accNames">
        <!-- 使用set标签注入简单set集合属性 -->
        <map>
            <entry key="9527" value="路飞"/>
            <entry key="9528" value="佐助"/>
            <entry key="9529"><value>鸣人 </value></entry>
        </map>
    </constructor-arg>
    <constructor-arg name="accounts">
        <!-- set标签注入引用类型属性 -->
        <map>
            <entry key="110"> <ref bean="account1"/></entry>
            <entry key="111"> <ref bean="account2"/></entry>
            <entry key="112" value-ref="account3"/>
        </map>
    </constructor-arg>
</bean>

2、同样我们可以在注入bean的同时实例化:

<constructor-arg name="accounts">
    <!-- set标签注入引用类型属性 -->
    <map>
        <entry key="110"> <ref bean="account1"/></entry>
        <entry key="111"> <ref bean="account2"/></entry>
        <entry key="112" value-ref="account3"/>
        <entry key="112">
            <bean class="com.boxuewa.dk.demo2.beans.Account">
                <constructor-arg name="account" value="翠花"/>
            </bean>
        </entry>
    </map>
</constructor-arg>

3、map的key可以是任何类型,所以其实map的可以也可使用 key-ref引用其他类型,当然就需要你自己制定一个专门的类型作为key。 这种方式我们一般都会使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

戴着假发的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值