spring依赖注入—数组类型和集合类型属性注入值

318 阅读4分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第5天,点击查看活动详情

1 为数组类型的属性赋值

Student类

public class Student {

    private Integer sid;

    private String sname;

    private Integer age;

    private String gender;

    private Double score;

    private String[] hobby;

    private Clazz clazz;

    //无参和有参构造方法
    //setget方法
    //toString

}

配置bean

如果你要配置的属性的数组类型的,那么你需要使用property标签的子标签array标签。如果你的数组元素的类型是字面量,那么你可以直接使用value标签;如果你的数组元素类型是类类型,那么你需要使用过ref标签,并用这个标签的bean属性引用对应的bean

<bean id="studentFive" class="com.atguigu.spring.pojo.Student">
    <property name="sid" value="1004"></property>
    <property name="sname" value="赵六"></property>
    <property name="age" value="26"></property>
    <property name="gender" value="男"></property>
    <property name="clazz">
        <!--内部bean,只能在当前bean的内部使用,不能直接通过IOC容器获取-->
        <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
            <property name="cid" value="2222"></property>
            <property name="cname" value="远大前程班"></property>
        </bean>
    </property>
    <property name="hobby">
        <array>
            <!--<ref bean=""></ref>-->
            <value>抽烟</value>
            <value>喝酒</value>
            <value>烫头</value>
        </array>
    </property>
</bean>

运行代码

@Test
public void testDI() {
    //获取IOC容器
    ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
    //获取bean
    Student student = ioc.getBean("studentFive", Student.class);
    System.out.println(student);
}

image-20221201161624730.png

2 为List集合类型的属性赋值

spring在IOC容器中为list集合类型的属性注入值有两种方式:

  1. 在对应的property标签里引用list标签,如果这个list集合是类类型的,比如list<Student>,你便需要在list标签里使用ref标签,用ref标签的bean属性将对应的bean的id填写进来。
  2. 可以在spring的配置文件里添加约束,这个约束是什么不需要去记,我们只需借助idea帮我们自动生成。具体做法是:先在配置文件里写上<util:list,接着根据idea的提示,自然会生成对应的约束-**xmlns:util="www.springframework.org/schema/util…util:list标签;<util:list>标签的id属性作用是作为一个唯一表示,<ref>标签是引入bean作为list集合的元素,<value>标签是引入字面量作为list集合的元素;最后在对应的property标签,通过ref属性引入<util:list>标签的id即可为list类型的属性注入集合类型的值。

Clazz类

public class Clazz {

    private Integer cid;
    private String cname;
    private List<Student> students;
    private List<String> strs;

    //无参和有参构造
    //set和get
    //toString
}

2.1 用<list>标签为list集合类型的属性注入值

配置bean

<bean id="studentOne" class="com.atguigu.spring.pojo.Student"></bean>

<bean id="studentTwo" class="com.atguigu.spring.pojo.Student">
    <!--property:通过成员变量的set方法进行赋值 name:指定属性名  value:为属性名注入的值-->
    <property name="sid" value="1001"></property>
    <property name="sname" value="张三"></property>
    <property name="age" value="23"></property>
    <property name="gender" value="男"></property>
</bean>

<bean id="studentThree" class="com.atguigu.spring.pojo.Student">
    <constructor-arg value="1002"></constructor-arg>
    <constructor-arg value="李四"></constructor-arg>
    <constructor-arg value="女"></constructor-arg>
    <constructor-arg value="24" name="age"></constructor-arg>
</bean>
<!--生成Clazz类型的bean-->
<bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
    <property name="cid" value="1111"></property>
    <property name="cname" value="最强王者班"></property>
    <property name="students">
        <list>
            <!--list集合的元素是类-->
            <ref bean="studentOne"></ref>
            <ref bean="studentTwo"></ref>
            <ref bean="studentThree"></ref>
        </list>
    </property>
    <property name="strs">
        <list>
            <!--list集合的元素是普通的字符串-->
            <value>测试字面量1</value>
            <value>测试字面量2</value>
            <value>测试字面量3</value>
        </list>
    </property>
</bean>

测试

@Test
public void testDI() {
    //获取IOC容器
    ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
    //获取bean
    Clazz clazz = ioc.getBean("clazzOne", Clazz.class);
    System.out.println(clazz);
}

Clazz{cid=1111, cname='最强王者班', students=[Student{sid=null, sname='null', age=null, gender='null', score=null, hobby=null, clazz=null}, Student{sid=1001, sname='张三', age=23, gender='男', score=null, hobby=null, clazz=null}, Student{sid=1002, sname='李四', age=24, gender='女', score=null, hobby=null, clazz=null}], strs=[测试字面量1, 测试字面量2, 测试字面量3]}

2.2 用<util:list>为list集合类型的属性注入值

配置bean

<!--生成Clazz类型的bean-->
<bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
    <property name="cid" value="1111"></property>
    <property name="cname" value="最强王者班"></property>
    <!--通过ref引入util:list的内容-->
    <property name="students" ref="studentList"></property>
    <!--<property name="students">
        <list>
            &lt;!&ndash;list集合的元素是类&ndash;&gt;
            <ref bean="studentOne"></ref>
            <ref bean="studentTwo"></ref>
            <ref bean="studentThree"></ref>
        </list>
    </property>-->
    <property name="strs" ref="stsList"></property>
    <!--<property name="strs">
        <list>
            &lt;!&ndash;list集合的元素是普通的字符串&ndash;&gt;
            <value>测试字面量1</value>
            <value>测试字面量2</value>
            <value>测试字面量3</value>
        </list>
    </property>-->
</bean>

<!--配置一个集合类型的bean,需要使用util的约束-->
<util:list id="studentList">
    <ref bean="studentOne"></ref>
    <ref bean="studentTwo"></ref>
    <ref bean="studentThree"></ref>
    <!--<ref bean="studentFive"></ref>-->
</util:list>
<util:list id="stsList">
    <value>紫米</value>
    <value>糯米</value>
    <value>大米</value>
</util:list>

测试结果

Clazz{cid=1111, cname='最强王者班', students=[Student{sid=null, sname='null', age=null, gender='null', score=null, hobby=null, clazz=null}, Student{sid=1001, sname='张三', age=23, gender='男', score=null, hobby=null, clazz=null}, Student{sid=1002, sname='李四', age=24, gender='女', score=null, hobby=null, clazz=null}], strs=[紫米, 糯米, 大米]}

如果是set集合类型属性赋值,将list标签换成set标签即可

3 为map集合类型的属性赋值

为map类型的属性进行依赖注入的方式也有两种:

  1. 在对应的property标签里使用map标签的子标签entry。entry标签有4个属性,分别是key,key-ref,value,value-ref。从属性的名字我们可以知道key和key-ref的作用是给map集合的键(key)赋值,前者是给String类型的键赋值,后者是给类类型的键赋值;value,value-ref的作用是给map集合的值(value)赋值,前者是给String类型的值赋值,后者是给类类型的值赋值。类类型都是在spring的IOC生成的,所以我们可以看到你在spring的配置文件引用它们时,对应的属性有ref的字样。

  2. <util:map>标签也可以用来给map类型的属性赋值。这个标签也需要使用entry子标签,entry子标签的内容和上面一样。

Teacher类

public class Teacher {

    private Integer tid;

    private String tname;

    //无参和有参构造器
    
    //set和get
    
    //toString
}

Student类再添加一些内容

private Map<String,Teacher> teacherMap;
//对应set和get方法
//重写toString

3.1 用<map>标签为map集合类型的属性注入值

<bean id="studentFive" class="com.atguigu.spring.pojo.Student">
    <property name="sid" value="1004"></property>
    <property name="sname" value="赵六"></property>
    <property name="age" value="26"></property>
    <property name="gender" value="男"></property>
    <property name="clazz">
        <!--内部bean,只能在当前bean的内部使用,不能直接通过IOC容器获取-->
        <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
            <property name="cid" value="2222"></property>
            <property name="cname" value="远大前程班"></property>
        </bean>
    </property>
    <property name="hobby">
        <array>
            <!--<ref bean=""></ref>-->
            <value>抽烟</value>
            <value>喝酒</value>
            <value>烫头</value>
        </array>
    </property>
    <!--为map集合类型注入值-->
    <property name="teacherMap">
        <map>
            <entry key="10086" value-ref="teacherOne"></entry>
            <entry key="10010" value-ref="teacherTwo"></entry>
        </map>
    </property>
</bean>

<bean id="teacherOne" class="com.atguigu.spring.pojo.Teacher">
    <property name="tid" value="10086"></property>
    <property name="tname" value="大宝"></property>
</bean>

<bean id="teacherTwo" class="com.atguigu.spring.pojo.Teacher">
    <property name="tid" value="10010"></property>
    <property name="tname" value="小宝"></property>
</bean>

测试

@Test
public void testDI() {
    //获取IOC容器
    ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
    //获取bean
    Student student = ioc.getBean("studentFive", Student.class);
    System.out.println(student);
}

Student{sid=1004, sname='赵六', age=26, gender='男', score=null, hobby=[抽烟, 喝酒, 烫头], clazz=Clazz{cid=2222, cname='远大前程班', students=null, strs=null}, teacherMap={10086=Teacher{tid=10086, tname='大宝'}, 10010=Teacher{tid=10010, tname='小宝'}}}

3.2 <util:map>为map集合类型的属性注入值

配置bean

<bean id="studentFive" class="com.atguigu.spring.pojo.Student">
    <property name="sid" value="1004"></property>
    <property name="sname" value="赵六"></property>
    <property name="age" value="26"></property>
    <property name="gender" value="男"></property>
    <property name="clazz">
        <!--内部bean,只能在当前bean的内部使用,不能直接通过IOC容器获取-->
        <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
            <property name="cid" value="2222"></property>
            <property name="cname" value="远大前程班"></property>
        </bean>
    </property>
    <property name="hobby">
        <array>
            <!--<ref bean=""></ref>-->
            <value>抽烟</value>
            <value>喝酒</value>
            <value>烫头</value>
        </array>
    </property>
    <!--引用util:map的内容-->
    <property name="teacherMap" ref="teacherMap"></property>
    <!--<property name="teacherMap">
            <map>
                <entry key="10086" value-ref="teacherOne"></entry>
                <entry key="10010" value-ref="teacherTwo"></entry>
            </map>
        </property>-->
</bean>

<util:map id="teacherMap">
    <entry key="10086" value-ref="teacherOne"></entry>
    <entry key="10010" value-ref="teacherTwo"></entry>
</util:map>

测试

Student{sid=1004, sname='赵六', age=26, gender='男', score=null, hobby=[抽烟, 喝酒, 烫头], clazz=Clazz{cid=2222, cname='远大前程班', students=null, strs=null}, teacherMap={10086=Teacher{tid=10086, tname='大宝'}, 10010=Teacher{tid=10010, tname='小宝'}}}