Spring中的集合注入代码实例

 更新时间:2023年11月01日 10:03:44   作者:端脑  
这篇文章主要介绍了Spring中的集合注入代码实例,集合注入是指在Spring框架中,通过配置文件或注解的方式将集合类型的数据注入到Bean中,集合类型包括List、Set、Map和Properties等,需要的朋友可以参考下

Spring中的集合注入

集合注入重要是对数组、List、Set、map的注入,具体注入方法请参照一下代码

重点是applicationContext.xml中对这几个集合注入的方式

代码看懂,你就会了

collection

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

    <bean id="department" class="com.chenny.entity.Department">
        <property name="name" value="财务部门" />
        <!-- 给数组注入值 -->
        <property name="empName">
            <array>
                <value>张三</value>
                <value>李四</value>
                <value>王五</value>
            </array>
        </property>

        <!-- 给list注入值 可以有相同的多个对象  -->
        <property name="empList">
            <list>
                <ref bean="emp1" />
                <ref bean="emp2"/>
                <ref bean="emp3"></ref>
            </list>
        </property>
        <!-- 给set注入值 不能有相同的对象 -->
        <property name="empSets">
            <set>
                <ref bean="emp1" />
                <ref bean="emp2"/>
                <ref bean="emp3"></ref>
            </set>
        </property>

        <!-- 给map注入值 只要map中的key值不一样就可以装配value -->
        <property name="empMap">
            <map>
                <entry key="1" value-ref="emp1" />
                <entry key="2" value-ref="emp2" />
                <entry key="3" value-ref="emp3"></entry>
            </map>
        </property>

        <!-- 给属性集合配置 -->
        <property name="pp">
            <props>
                <prop key="pp1">hello</prop>
                <prop key="pp2">world</prop>
            </props>
        </property>
    </bean>


    <bean id="emp1" class="com.chenny.entity.Employee">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
    </bean>
    <bean id="emp2" class="com.chenny.entity.Employee">
        <property name="id" value="2"></property>
        <property name="name" value="李四"></property>
    </bean>

    <bean id="emp3" class="com.chenny.entity.Employee">
        <property name="id" value="3"></property>
        <property name="name" value="王五"></property>
    </bean>

</beans>

Testbean

package com.chenny.test;

import com.chenny.entity.Department;
import com.chenny.entity.Employee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

public class TestBean {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("collection.xml");
        Department department = (Department) applicationContext.getBean("department");
        System.out.println("*********输出全部的department的信息******");
        System.out.println(department);

        System.out.println("*********取出数组中,部门人员姓名的全部数据******");
        for(String emName : department.getEmpName()){
            System.out.println(emName);
        }
        System.out.println("*********通过List集合取出数据******");
        for(Employee e : department.getEmpList()){
            System.out.println("id" + e.getId() +"name = "+ e.getName());
        }

        System.out.println("*********通过Set集合取出数据******");
        for(Employee e : department.getEmpSets()){
            System.out.println("id = " + e.getId() +", name = "+ e.getName());
        }

        System.out.println("*********通过Map集合取出数据(迭代器方法)******");
        //迭代器
        Map<String,Employee> empMap = department.getEmpMap();
        Iterator it = empMap.keySet().iterator();
        while(it.hasNext()){
            String key = (String) it.next();
            Employee emp = empMap.get(key);
            System.out.println("key = " + key + ", id = " + emp.getId() + ", name = " + emp.getName() );
        }

        System.out.println("*********通过Map集合取出数据(Emtry简洁法)******");
        //简洁方法
        for(Map.Entry<String,Employee> entry : department.getEmpMap().entrySet()){

            System.out.println("key = " + entry.getKey()+ " id = " + entry.getValue().getId() + " name = " + entry.getValue().getName());
        }


        System.out.println("*********通过Propertis取出数据(通过Entry对象取)******");
        Properties pp = department.getPp();
        for(Map.Entry<Object,Object> entry : pp.entrySet()){
            System.out.println(entry.getKey().toString() + ", "+ entry.getValue().toString());
        }


        System.out.println("*********通过Propertis取出数据(通过Enumeration对象取)******");
        Enumeration en = pp.keys();
        while(en.hasMoreElements()){
            String key = (String) en.nextElement();
            System.out.println(key + " " + pp.getProperty(key));
        }
    }
}

Department

package com.chenny.entity;

import java.util.*;

/**
 * @author 73981
 */
public class Department {
    private String name;
    private String[] empName;
    private List<Employee> empList;
    private Set<Employee> empSets;
    private Map<String,Employee> empMap;
    private Properties pp;

    @Override
    public String toString() {
        return "Department{" +
                "name='" + name + '\'' +
                ", empName=" + Arrays.toString(empName) +
                ", empList=" + empList +
                ", empSets=" + empSets +
                ", empMap=" + empMap +
                ", pp=" + pp +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String[] getEmpName() {
        return empName;
    }

    public void setEmpName(String[] empName) {
        this.empName = empName;
    }

    public List<Employee> getEmpList() {
        return empList;
    }

    public void setEmpList(List<Employee> empList) {
        this.empList = empList;
    }

    public Set<Employee> getEmpSets() {
        return empSets;
    }

    public void setEmpSets(Set<Employee> empSets) {
        this.empSets = empSets;
    }

    public Map<String, Employee> getEmpMap() {
        return empMap;
    }

    public void setEmpMap(Map<String, Employee> empMap) {
        this.empMap = empMap;
    }

    public Properties getPp() {
        return pp;
    }

    public void setPp(Properties pp) {
        this.pp = pp;
    }

    public Department() {
    }

    public Department(String name, String[] empName, List<Employee> empList, Set<Employee> empSets, Map<String, Employee> empMap, Properties pp) {
        this.name = name;
        this.empName = empName;
        this.empList = empList;
        this.empSets = empSets;
        this.empMap = empMap;
        this.pp = pp;
    }
}

Employee

package com.chenny.entity;

public class Employee {
    private String name;
    private int id;

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Employee() {
    }

    public Employee(String name, int id) {
        this.name = name;
        this.id = id;
    }
}

到此这篇关于Spring中的集合注入代码实例的文章就介绍到这了,更多相关Spring中的集合注入内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用Jackson反序列化遇到的问题及解决

    使用Jackson反序列化遇到的问题及解决

    这篇文章主要介绍了使用Jackson反序列化遇到的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Java设计模式中的策略模式详细解析

    Java设计模式中的策略模式详细解析

    这篇文章主要介绍了Java设计模式中的策略模式详细解析,所谓策略模式,指的是做某一件事时有多种选择(即策略),且不同的策略之间相互独立,而且无论使用哪种策略,得到的结果都是相同的,需要的朋友可以参考下
    2023-12-12
  • Spring Profiles使用方法详解

    Spring Profiles使用方法详解

    在你刚接触SpringBoot的时候有没有对它提供的Profile有些许不适应,经过摸索后才领悟到它的强大。今天我就对Profile进行一点归纳总结,留作互联网记忆
    2022-12-12
  • springboot实现rabbitmq的队列初始化和绑定

    springboot实现rabbitmq的队列初始化和绑定

    这篇文章主要介绍了springboot实现rabbitmq的队列初始化和绑定,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-10-10
  • MyBatis 执行动态 SQL语句详解

    MyBatis 执行动态 SQL语句详解

    大家对mybatis执行任意sql语句都了解,那么MyBatis执行动态SQL语句呢?下面脚本之家小编给大家解答下mybatis执行动态sql语句的方法,非常不错,感兴趣的朋友参考下吧
    2016-08-08
  • Java时区转换实例代码解析

    Java时区转换实例代码解析

    这篇文章主要介绍了Java时区转换实例代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • Java 并发编程ArrayBlockingQueue的实现

    Java 并发编程ArrayBlockingQueue的实现

    这篇文章主要介绍了Java 并发编程ArrayBlockingQueue的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • 浅谈java的TCP和UDP编程(附实例讲解)

    浅谈java的TCP和UDP编程(附实例讲解)

    下面小编就为大家带来一篇浅谈java的TCP和UDP编程(附实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • Java超详细讲解多线程中的Process与Thread

    Java超详细讲解多线程中的Process与Thread

    进程process:在一定的环境下,把静态的程序代码运行起来,通过使用不同的资源,来完成一定的任务;线程thread:是程序中一个单一的顺序控制流程。在单个进程中同时运行多个线程完成不同的工作,称为多线程
    2022-05-05
  • Mockito mock Kotlin Object类方法报错解决方法

    Mockito mock Kotlin Object类方法报错解决方法

    这篇文章主要介绍了Mockito mock Kotlin Object类方法报错解决方法,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-09-09

最新评论