Spring 注入集合类型

文章展示了如何在Spring中对集合类型如数组、List、Map和Set进行属性注入,通过XML配置文件进行详细配置,包括定义集合属性,设置方法以及具体注入值的过程。此外,还提及了引入util名称空间来更方便地处理集合注入的情况。
摘要由CSDN通过智能技术生成

使用 Spring 注入集合类型

注入集合(数组、List、Map、Set)类型属性
(1)创建类,定义数组,list,map,set类型属性,并且生成对应的set方法。
(2)在spring配置文件中进行配置。

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


public class Stu {

    //1、数组类型属性
    private String[] courses;

    //2、list集合类型属性
    private List<String> list;

    //3、map集合类型属性
    private Map<String,String> maps;

    //4、set集合类型属性
    private Set<String> sets;


    //学生所学的多门课程
    private List<Course> courseList;

    public void setCourseList(List<Course> courseList) {
        this.courseList = courseList;
    }

    public void setCourses(String[] courses) {
        this.courses = courses;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }

    public void setSets(Set<String> sets) {
        this.sets = sets;
    }

    public void test(){
        System.out.println(Arrays.toString(courses));
        System.out.println(list);
        System.out.println(maps);
        System.out.println(sets);
        System.out.println(courseList);
    }

}

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.xsd">

    <!--集合类型属性注入-->
    <bean id="stu" class="com.spring5.collectiontype.Stu">
        <!--数组类型属性注入-->
        <property name="courses">
            <array>
                <value>Java</value>
                <value>C++</value>
                <value>Python</value>
            </array>
        </property>

        <!--list类型属性注入-->
        <property name="list">
            <list>
                <value>小明</value>
                <value>小红</value>
            </list>
        </property>

        <!--map类型属性注入-->
        <property name="maps">
            <map>
                <entry key="Java" value="java"></entry>
                <entry key="C++" value="c++"></entry>
            </map>
        </property>

        <!--set类型属性注入-->
        <property name="sets">
            <set>
                <value>北京</value>
                <value>上海</value>
            </set>
        </property>

        <!--注入list集合类型,值是对象-->
        <property name="courseList">
            <list>
                <ref bean="course1"></ref>
                <ref bean="course2"></ref>
            </list>
        </property>


    </bean>


    <!--创建多个course对象-->
    <bean id="course1" class="com.spring5.collectiontype.Course">
        <property name="cname" value="Spring5框架"></property>
    </bean>

    <bean id="course2" class="com.spring5.collectiontype.Course">
        <property name="cname" value="MyBatis框架"></property>
    </bean>


</beans>

测试类

public class TestSpring {

    @Test
    public void testCollection1(){

        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        Stu stu = context.getBean("stu",Stu.class);
        stu.test();


    }

}

把集合注入部分提取出来

(1)在spring配置文件中引入名称空间util(
在配置信息中添加xmlns:util=“http://www.springframework.org/schema/util"和http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd”)。
(2)提取list集合类型属性注入。
(3)把提取的list集合类型属性注入使用。

package com.spring5.collectiontype;

import java.util.List;


public class Book {
    private List<String> list;

    public void setList(List<String> list) {
        this.list = list;
    }
    public void test(){
        System.out.println(list);
    }
}

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"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <!--把集合注入部分提取出来-->

    <!--1、提取list集合类型属性注入-->
    <util:list id="bookList">
        <value>老人与海</value>
        <value>平凡的世界</value>
        <value>阿甘正传</value>
    </util:list>

    <!--2、提取list集合类型属性注入使用-->
    <bean id="book" class="com.spring5.collectiontype.Book">
        <property name="list" ref="bookList"></property>
    </bean>

</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一雨曦一

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

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

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

打赏作者

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

抵扣说明:

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

余额充值