Spring中集合类型属性注入

时间:2022-09-29 11:46:02

我们都知道如何去注入普通属性的值,非常简单,那么我们如何去注入开发中常见的集合类型的属性了,别急,往下看。

这里将介绍如何给Map list set Array Properties 这些属性注入值。

1.创建一个类:员工类Employee

package cn.entity;

/**
 * 员工类
 *
 * @author hyj
 *
 */
public class Employee {
    //员工年龄
    private Integer age;
    //员工姓名
    private String name;

    public Employee() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Employee(Integer age, String name) {
        super();
        this.age = age;
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

}

2.创建第二个类里面包含上面所包含的集合和数组

package cn.collection;

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

import cn.entity.Employee;

public class Collection {
    private String [] empName;//数组
    private List<Employee> empList;//list集合
    private Set<Employee> empsets;//set集合
    private Map<String,Employee> empMaps;//map集合
    private Properties pp;//Properties的使用
    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> getEmpMaps() {
        return empMaps;
    }
    public void setEmpMaps(Map<String, Employee> empMaps) {
        this.empMaps = empMaps;
    }
    public Properties getPp() {
        return pp;
    }
    public void setPp(Properties pp) {
        this.pp = pp;
    }

}

3.创建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="emp1" class="cn.entity.Employee">
      <property name="age" value="18"></property>
      <property name="name" value="张三"></property>
  </bean>
   <bean id="emp2" class="cn.entity.Employee">
      <property name="age" value="18"></property>
      <property name="name" value="李四"></property>
  </bean>

  <!-- Collection对象-->
  <bean id="collection" class="cn.collection.Collection">
      <!-- 给数组赋值 -->
      <property name="empName">
           <list>
             <value>张三</value>
             <value>李四</value>
             <value>王五</value>
           </list>
      </property>
      <!-- 给list集合赋值 -->
      <property name="empList">
         <list>
           <ref bean="emp1"/>
           <ref bean="emp2"/>
         </list>
      </property>
      <!-- 给set集合赋值 -->
      <property name="empsets">
         <set>
           <ref bean="emp1"/>
           <ref bean="emp2"/>

         </set>
      </property>
      <!-- 给Map集合赋值 -->
      <property name="empMaps">
         <map>
           <entry key="001" value-ref="emp1"></entry>
           <entry key="002" value-ref="emp2"></entry>
         </map>
      </property>
      <!-- 给Properties集合赋值 -->
      <property name="pp">
        <props>
            <prop key="110" >hello</prop>
            <prop key="120">Spring</prop>
        </props>
      </property>

  </bean>

</beans>

4.测试类:

package cn.test;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.collection.Collection;
import cn.entity.Employee;

public class TestHappy {

    /**
     * 给数组赋值
     */
    @Test
    public void setArray(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Collection collection=(Collection)context.getBean("collection");
        for (String item : collection.getEmpName()) {
            System.out.println(item);
        }
    }
    /**
     * 给list集合赋值
     */
    @Test
    public void  setList(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Collection collection=(Collection)context.getBean("collection");
        for (Employee item : collection.getEmpList()) {
            System.out.println("年龄:"+item.getAge()+"姓名:"+item.getName());
        }

    }

    /**
     * 给set集合赋值
     */
    @Test
    public void  setSet(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Collection collection=(Collection)context.getBean("collection");

        for (Employee item : collection.getEmpsets()) {
            System.out.println("年龄:"+item.getAge()+"姓名:"+item.getName());
        }

    }

    /**
     * 给map集合赋值
     */
    @Test
    public  void setMap(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Collection collection=(Collection)context.getBean("collection");
        Map<String, Employee> map=collection.getEmpMaps();
        Iterator<String> iterator=map.keySet().iterator();
        while (iterator.hasNext()) {
            Employee employee=map.get(iterator.next());
            System.out.println("年龄:"+employee.getAge()+"姓名:"+employee.getName());
        }
    }

    @Test
    /**
     * Properties集合
     */
    public void testSet(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Collection collection=(Collection)context.getBean("collection");
        for(Entry<Object,Object> entry: collection.getPp().entrySet()){
            System.out.println(entry.getKey().toString()+" "+entry.getValue().toString());
        }

    }

}