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注入集合类型属性的全部内容。


相关文章
|
1天前
|
存储 JSON NoSQL
redis基本数据结构(String,Hash,Set,List,SortedSet)【学习笔记】
这篇文章是关于Redis基本数据结构的学习笔记,包括了String、Hash、Set、List和SortedSet的介绍和常用命令。文章解释了每种数据结构的特点和使用场景,并通过命令示例演示了如何在Redis中操作这些数据结构。此外,还提供了一些练习示例,帮助读者更好地理解和应用这些数据结构。
redis基本数据结构(String,Hash,Set,List,SortedSet)【学习笔记】
|
1月前
|
XML Java 数据格式
Spring5入门到实战------4、IOC容器-Bean管理XML方式、集合的注入(二)
这篇文章是Spring5框架的实战教程,主题是IOC容器中Bean的集合属性注入,通过XML配置方式。文章详细讲解了如何在Spring中注入数组、List、Map和Set类型的集合属性,并提供了相应的XML配置示例和Java类定义。此外,还介绍了如何在集合中注入对象类型值,以及如何使用Spring的util命名空间来实现集合的复用。最后,通过测试代码和结果展示了注入效果。
Spring5入门到实战------4、IOC容器-Bean管理XML方式、集合的注入(二)
|
1月前
|
存储 安全 Java
java集合框架复习----(4)Map、List、set
这篇文章是Java集合框架的复习总结,重点介绍了Map集合的特点和HashMap的使用,以及Collections工具类的使用示例,同时回顾了List、Set和Map集合的概念和特点,以及Collection工具类的作用。
java集合框架复习----(4)Map、List、set
|
1月前
|
存储 NoSQL 算法
Redis6入门到实战------ 三、常用五大数据类型(列表(List)、集合(Set)、哈希(Hash)、Zset(sorted set))
这是关于Redis 6入门到实战的文章,具体内容涉及Redis的五大数据类型:列表(List)、集合(Set)、哈希(Hash)、有序集合(Zset(sorted set))。文章详细介绍了这些数据类型的特点、常用命令以及它们背后的数据结构。如果您有任何关于Redis的具体问题或需要进一步的帮助,请随时告诉我。
|
30天前
|
缓存 Java 数据库连接
Spring Boot 资源文件属性配置,紧跟技术热点,为你的应用注入灵动活力!
【8月更文挑战第29天】在Spring Boot开发中,资源文件属性配置至关重要,它让开发者能灵活定制应用行为而不改动代码,极大提升了可维护性和扩展性。Spring Boot支持多种配置文件类型,如`application.properties`和`application.yml`,分别位于项目的resources目录下。`.properties`文件采用键值对形式,而`yml`文件则具有更清晰的层次结构,适合复杂配置。此外,Spring Boot还支持占位符引用和其他外部来源的属性值,便于不同环境下覆盖默认配置。通过合理配置,应用能快速适应各种环境与需求变化。
33 0
|
30天前
|
安全 Java 开发者
开发者必看!@Resource与private final的较量,Spring Boot注入技巧大揭秘,你不可不知的细节!
【8月更文挑战第29天】Spring Boot作为热门Java框架,其依赖注入机制备受关注。本文通过对比@Resource(JSR-250规范)和@Autowired(Spring特有),并结合private final声明的字段注入,详细探讨了两者的区别与应用场景。通过示例代码展示了@Resource按名称注入及@Autowired按类型注入的特点,并分析了它们在注入时机、依赖性、线程安全性和单一职责原则方面的差异,帮助开发者根据具体需求选择最合适的注入策略。
34 0
|
1月前
|
存储 Java 索引
|
1月前
|
测试技术 索引 Python
Python接口自动化测试框架(基础篇)-- 常用数据类型list&set()
本文介绍了Python中list和set两种数据类型的使用,包括它们的创建、取值、增删改查操作、排序以及内置函数的使用,还探讨了list的比较函数和set的快速去重功能。
19 0
|
2月前
|
消息中间件 JSON NoSQL
Redis深度解析:核心数据类型之hash、list、set
Redis深度解析:核心数据类型之hash、list、set
|
2月前
|
Java Spring
spring注入的几种方式
spring注入的几种方式
19 0