Spring_04_依赖注入_04_List、Set和Map类型注入

在有些类中属性类型相对来讲比较复杂,除了需要进行引用的实体类外,还有List、Set和Map等类型,这就要求在Spring的配置文件中需要进行相应的配置处理。

1:对于需要引用的其他Java类,可以使用属性标签“property”中的“ref”属性进行赋值,值为已编配到Spring容器中Bean的“id”值来实现;
2:对于List、Set和Map等类型的数据参数可以通过属性标签“property”中的“list”、“set”、“map”和“props”等标签来进行数据注入实现。

4.4.1 编写JavaBean的实体类

编写两个普通的Java实体类:
宠物类:

package com.marshal.spring.di.listsetmap;

public class Pet {

    private String name;
    private String color;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Pet{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                ", age=" + age +
                '}';
    }
}

在该类中只有三个属性。
神仙类:

package com.marshal.spring.di.listsetmap;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Immortal {

    private String name;
    private String sex;
    private Pet pet;
    private List clothes;
    private Set weapon;
    private Map tasks;
    private Properties residence;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Pet getPet() {
        return pet;
    }

    public void setPet(Pet pet) {
        this.pet = pet;
    }

    public List getClothes() {
        return clothes;
    }

    public void setClothes(List clothes) {
        this.clothes = clothes;
    }

    public Set getWeapon() {
        return weapon;
    }

    public void setWeapon(Set weapon) {
        this.weapon = weapon;
    }

    public Map getTasks() {
        return tasks;
    }

    public void setTasks(Map tasks) {
        this.tasks = tasks;
    }

    public Properties getResidence() {
        return residence;
    }

    public void setResidence(Properties residence) {
        this.residence = residence;
    }

    @Override
    public String toString() {
        return "Immortal{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", pet=" + pet +
                ", clothes=" + clothes +
                ", weapon=" + weapon +
                ", tasks=" + tasks +
                ", residence=" + residence +
                '}';
    }
}

在该类中包括类属性以及List、Set、Map、Properties等类型属性各一个。

4.4.2 编写该类的配置文件

编写配置文件如下:

<?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="pet" class="com.marshal.spring.di.listsetmap.Pet">
        <property name="name" value="哮天犬"/>
        <property name="color" value="黑色"/>
        <property name="age" value="1000000"/>
    </bean>

    <bean id="immortal" class="com.marshal.spring.di.listsetmap.Immortal">
        <property name="name" value="二郎神"/>
        <property name="sex" value=""/>
        <property name="pet" ref="pet"/>
        <property name="clothes">
            <list>
                <value>银盔</value>
                <value>战甲</value>
                <value>战靴</value>
                <value>披风</value>
                <ref bean="pet"/>
            </list>
        </property>
        <property name="weapon">
            <set>
                <value>三叉抢</value>
                <value>盾牌</value>
                <ref bean="pet"/>
            </set>
        </property>
        <property name="tasks">
            <map>
                <entry key="a" value="捉拿孙悟空"/>
                <entry key="b" value="朋友家喝酒"/>
            </map>
        </property>
        <property name="residence">
            <props>
                <prop key="a">洞府</prop>
                <prop key="b">神庙</prop>
            </props>
        </property>

    </bean>

</beans>

在该配置文件中,包括对象引用、List、Set、Map、Properties等数据的配置。

4.4.3 编写启动容器测试类

创建测试类“MainTest”,通过该类中的main方法进行测试:
测试类
在容器中获取该类的实例后进行该类的实例打印,调用该类重写的“toString”方法:

类的实例打印

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值