Spring注入集合(数组、List、Map、Set)类型属性

简介: Spring注入集合(数组、List、Map、Set)类型属性

注入集合(数组、List、Map、Set)类型属性


(1)创建类,定义数组,list,map,set类型属性,并且生成对应的set方法。

(2)在spring配置文件中进行配置。


Stu类:


package com.Keafmd.spring5.collectiontype;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * Keafmd
 *
 * @ClassName: Stu
 * @Description:  IOC操作Bean管理(xml注入属性集合)
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:15
 */
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);
    }
}

bean1.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.Keafmd.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.Keafmd.spring5.collectiontype.Course">
        <property name="cname" value="Spring5框架"></property>
    </bean>
    <bean id="course2" class="com.Keafmd.spring5.collectiontype.Course">
        <property name="cname" value="MyBatis框架"></property>
    </bean>
</beans>

测试类:


package com.Keafmd.spring5.testdemo;
import com.Keafmd.spring5.bean.Orders;
import com.Keafmd.spring5.collectiontype.Book;
import com.Keafmd.spring5.collectiontype.Course;
import com.Keafmd.spring5.collectiontype.Stu;
import com.Keafmd.spring5.factorybean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Keafmd
 *
 * @ClassName: TestSpring5demo1
 * @Description: 测试类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:30
 */
public class TestSpring5demo1 {
    @Test
    public void testCollection1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        Stu stu = context.getBean("stu",Stu.class);
        stu.test();
    }
}

输出结果:


[Java, C++, Python]
[小明, 小红]
{Java=java, C++=c++}
[北京, 上海]
[Course{cname='Spring5框架'}, Course{cname='MyBatis框架'}]
Process finished with exit code 0


把集合注入部分提取出来


(1)在spring配置文件中引入名称空间util(

在配置信息中添加xmlns:util="http://www.springframework.org/schema/util"http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsd"

(2)提取list集合类型属性注入。

(3)把提取的list集合类型属性注入使用。


Book类:


package com.Keafmd.spring5.collectiontype;
import java.util.List;
/**
 * Keafmd
 *
 * @ClassName: Book
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:56
 */
public class Book {
    private List<String> list;
    public void setList(List<String> list) {
        this.list = list;
    }
    public void test(){
        System.out.println(list);
    }
}

bean2.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.Keafmd.spring5.collectiontype.Book">
        <property name="list" ref="bookList"></property>
    </bean>
</beans>

测试代码:


package com.Keafmd.spring5.testdemo;
import com.Keafmd.spring5.bean.Orders;
import com.Keafmd.spring5.collectiontype.Book;
import com.Keafmd.spring5.collectiontype.Course;
import com.Keafmd.spring5.collectiontype.Stu;
import com.Keafmd.spring5.factorybean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Keafmd
 *
 * @ClassName: TestSpring5demo1
 * @Description: 测试类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-15 14:30
 */
public class TestSpring5demo1 {
    @Test
    public void testCollection2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        Book book = context.getBean("book",Book.class);
        book.test();
    }
}

输出结果:


[老人与海, 平凡的世界, 阿甘正传]
Process finished with exit code 0

以上就是Spring注入集合类型属性的全部内容。


相关文章
|
2月前
|
Java 测试技术 微服务
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——少量配置信息的情形
本课主要讲解Spring Boot项目中的属性配置方法。在实际开发中,测试与生产环境的配置往往不同,因此不应将配置信息硬编码在代码中,而应使用配置文件管理,如`application.yml`。例如,在微服务架构下,可通过配置文件设置调用其他服务的地址(如订单服务端口8002),并利用`@Value`注解在代码中读取这些配置值。这种方式使项目更灵活,便于后续修改和维护。
38 0
|
3月前
|
XML Java 测试技术
Spring AOP—通知类型 和 切入点表达式 万字详解(通俗易懂)
Spring 第五节 AOP——切入点表达式 万字详解!
193 25
|
2月前
|
Java 数据库 微服务
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——指定项目配置文件
在实际项目中,开发环境和生产环境的配置往往不同。为简化配置切换,可通过创建 `application-dev.yml` 和 `application-pro.yml` 分别管理开发与生产环境配置,如设置不同端口(8001/8002)。在 `application.yml` 中使用 `spring.profiles.active` 指定加载的配置文件,实现环境快速切换。本节还介绍了通过配置类读取参数的方法,适用于微服务场景,提升代码可维护性。课程源码可从 [Gitee](https://gitee.com/eson15/springboot_study) 下载。
68 0
|
2月前
|
Java 微服务 Spring
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——少量配置信息的情形
在微服务架构中,随着业务复杂度增加,项目可能需要调用多个微服务。为避免使用`@Value`注解逐一引入配置的繁琐,可通过定义配置类(如`MicroServiceUrl`)并结合`@ConfigurationProperties`注解实现批量管理。此方法需在配置文件中设置微服务地址(如订单、用户、购物车服务),并通过`@Component`将配置类纳入Spring容器。最后,在Controller中通过`@Resource`注入配置类即可便捷使用,提升代码可维护性。
43 0
使用 entrySet 遍历 Map 类集合 KV
使用 entrySet 遍历 Map 类集合 KV
|
5月前
|
算法
你对Collection中Set、List、Map理解?
你对Collection中Set、List、Map理解?
117 18
你对Collection中Set、List、Map理解?
|
5月前
|
存储 缓存 安全
只会“有序无序”?面试官嫌弃的List、Set、Map回答!
小米,一位热衷于技术分享的程序员,通过与朋友小林的对话,详细解析了Java面试中常见的List、Set、Map三者之间的区别,不仅涵盖了它们的基本特性,还深入探讨了各自的实现原理及应用场景,帮助面试者更好地准备相关问题。
108 20
|
6月前
|
Java 开发者 Spring
Spring高手之路24——事务类型及传播行为实战指南
本篇文章深入探讨了Spring中的事务管理,特别是事务传播行为(如REQUIRES_NEW和NESTED)的应用与区别。通过详实的示例和优化的时序图,全面解析如何在实际项目中使用这些高级事务控制技巧,以提升开发者的Spring事务管理能力。
132 1
Spring高手之路24——事务类型及传播行为实战指南
|
8月前
|
存储 前端开发 API
ES6的Set和Map你都知道吗?一文了解集合和字典在前端中的应用
该文章详细介绍了ES6中Set和Map数据结构的特性和使用方法,并探讨了它们在前端开发中的具体应用,包括如何利用这些数据结构来解决常见的编程问题。
ES6的Set和Map你都知道吗?一文了解集合和字典在前端中的应用
|
8月前
|
算法
你对Collection中Set、List、Map理解?
你对Collection中Set、List、Map理解?
73 5