spring中List,Set,Map集合的输出(详解)

简介: spring中List,Set,Map集合的输出(详解)

注入依赖:

注入:bean的所有属性都由容器来注入

依赖:bean的所有配置都离不开容器

附:学习spring可以直接看文档

基于setter配置

1,使用list将集合输出(需要的jar包上一章有),主要区分value和ref的使用区别

value:即字面量属性

ref:非字面量属性

接下来看代码,新建两个javabean,建在pojo包下

代码分别为

Student.java

package pojo;
public class Student {
    private String name;
    private Integer id;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    @Override
    public String toString() {
        return "name=" + name + "," + "id=" + id;
    }
}

UserQaq

package pojo;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class UserQaq {
    private String name;
    private Integer age;
    private List<Student> list1;
    private List<Integer> list2;
    private Set<Student> set1;
    private Set<String> set2;
    private Map<Integer,String> map1;
    public Map<String, Student> getMap2() {
        return map2;
    }
    public void setMap2(Map<String, Student> map2) {
        this.map2 = map2;
    }
    private Map<String,Student> map2;
    public Map<Integer, String> getMap1() {
        return map1;
    }
    public void setMap1(Map<Integer, String> map1) {
        this.map1 = map1;
    }
    public Set<Student> getSet1() {
        return set1;
    }
    public void setSet1(Set<Student> set1) {
        this.set1 = set1;
    }
    public Set<String> getSet2() {
        return set2;
    }
    public void setSet2(Set<String> set2) {
        this.set2 = set2;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public List<Student> getList1() {
        return list1;
    }
    public void setList1(List<Student> list1) {
        this.list1 = list1;
    }
    public List<Integer> getList2() {
        return list2;
    }
    public void setList2(List<Integer> list2) {
        this.list2 = list2;
    }
    @Override
    public String toString() {
        return "name = " + name + "\n" + "age=" + age + "\n" + "list1 = " + list1 + "\n" + "list2" + list2
                + "\n" + "set1=" + set1 + "\n" + "set2" + set2 + "\n" + "map1=" + map1 + "\n" + "map2" + map2;
    }
}

在容器中配置xml文件,新建一个applicationContext.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="userQaq" class="pojo.UserQaq">
        <property name="name" value="蔡康程"></property>
        <property name="age" value="21"></property>
        <property name="list1">
            <!--非字面量属性ref,如此属性,不能写成字符串的属性-->
            <list>
                <ref bean="student1"></ref>
                <ref bean="student2"></ref>
                <ref bean="student3"></ref>
                <ref bean="student4"></ref>
                <ref bean="student5"></ref>
            </list>
        </property>
        <property name="list2">
            <list>
                <!--字面量属性使用value-->
                <value>1234</value>
                <value>2345</value>
                <value>3456</value>
                <value>4567</value>
            </list>
        </property>
        <property name="set1">
            <set>
                <ref bean="student1"></ref>
                <ref bean="student2"></ref>
                <ref bean="student3"></ref>
                <ref bean="student4"></ref>
                <ref bean="student5"></ref>
            </set>
        </property>
        <property name="set2">
            <set>
                <value>qaq</value>
                <value>qqq</value>
                <value>qwq</value>
                <value>qeq</value>
                <value>qrq</value>
            </set>
        </property>
        <property name="map1">
            <map>
                <entry key="123" value="红楼梦"></entry>
                <entry key="456" value="三国演义"></entry>
            </map>
        </property>
        <property name="map2">
            <map>
                <entry key="123" value-ref="student1"></entry>
                <entry key="456" value-ref="student2"></entry>
                <entry key="123" value-ref="student3"></entry>
                <entry key="456" value-ref="student4"></entry>
                <entry key="123" value-ref="student5"></entry>
            </map>
        </property>
    </bean>
    <bean id="student1" class="pojo.Student">
        <property name="id" value="123"></property>
        <property name="name" value="李海乐"></property>
    </bean>
    <bean id="student2" class="pojo.Student">
        <property name="id" value="133"></property>
        <property name="name" value="钟华金"></property>
    </bean>
    <bean id="student3" class="pojo.Student">
        <property name="id" value="143"></property>
        <property name="name" value="易景亮"></property>
    </bean>
    <bean id="student4" class="pojo.Student">
        <property name="id" value="153"></property>
        <property name="name" value="渣男"></property>
    </bean>
    <bean id="student5" class="pojo.Student">
        <property name="id" value="163"></property>
        <property name="name" value="万翀"></property>
    </bean>
</beans>

接下来写测试类

package tool;
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        import pojo.UserQaq;
public class TestUserQaq {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserQaq userQaq = (UserQaq) context.getBean("userQaq");
        System.out.println(userQaq);
    }
}

最后可以发现,在idea中出现此标志

测试成功!

相关文章
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
761 2
|
存储 缓存 JavaScript
Set和Map有什么区别?
Set和Map有什么区别?
1019 1
|
存储 JavaScript 前端开发
for...of循环在遍历Set和Map时的注意事项有哪些?
for...of循环在遍历Set和Map时的注意事项有哪些?
1009 156
|
存储 算法 容器
set_map的实现+set/map加持秒杀高频算法题锻炼算法思维
`set`基于红黑树实现,支持有序存储、自动去重,增删查效率为O(logN)。通过仿函数可自定义排序规则,配合空间配置器灵活管理内存。不支持修改元素值,迭代器失效需注意。`multiset`允许重复元素。常用于去重、排序及查找场景。
|
存储 Java 索引
(Python基础)新时代语言!一起学习Python吧!(二):字符编码由来;Python字符串、字符串格式化;list集合和tuple元组区别
字符编码 我们要清楚,计算机最开始的表达都是由二进制而来 我们要想通过二进制来表示我们熟知的字符看看以下的变化 例如: 1 的二进制编码为 0000 0001 我们通过A这个字符,让其在计算机内部存储(现如今,A 字符在地址通常表示为65) 现在拿A举例: 在计算机内部 A字符,它本身表示为 65这个数,在计算机底层会转为二进制码 也意味着A字符在底层表示为 1000001 通过这样的字符表示进行转换,逐步发展为拥有127个字符的编码存储到计算机中,这个编码表也被称为ASCII编码。 但随时代变迁,ASCII编码逐渐暴露短板,全球有上百种语言,光是ASCII编码并不能够满足需求
548 4
|
安全 Java 数据库连接
让我们讲解一下 Map 集合遍历的方式
我是小假 期待与你的下一次相遇 ~
559 43
|
并行计算 Java API
Java List 集合结合 Java 17 新特性与现代开发实践的深度解析及实战指南 Java List 集合
本文深入解析Java 17中List集合的现代用法,结合函数式编程、Stream API、密封类、模式匹配等新特性,通过实操案例讲解数据处理、并行计算、响应式编程等场景下的高级应用,帮助开发者提升集合操作效率与代码质量。
728 1
|
存储 安全 Java
Java 学习路线 35 掌握 List 集合从入门到精通的 List 集合核心知识
本文详细解析Java中List集合的原理、常用实现类(如ArrayList、LinkedList)、核心方法及遍历方式,并结合数据去重、排序等实际应用场景,帮助开发者掌握List在不同业务场景下的高效使用,提升Java编程能力。
929 0
|
存储 缓存 JavaScript
在JavaScript中,Set和Map的性能有什么区别?
在JavaScript中,Set和Map的性能有什么区别?
828 0
|
存储 C++ 容器
unordered_set、unordered_multiset、unordered_map、unordered_multimap的介绍及使用
unordered_set是不按特定顺序存储键值的关联式容器,其允许通过键值快速的索引到对应的元素。在unordered_set中,元素的值同时也是唯一地标识它的key。在内部,unordered_set中的元素没有按照任何特定的顺序排序,为了能在常数范围内找到指定的key,unordered_set将相同哈希值的键值放在相同的桶中。unordered_set容器通过key访问单个元素要比set快,但它通常在遍历元素子集的范围迭代方面效率较低。它的迭代器至少是前向迭代器。前向迭代器的特性。
939 0