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

这里将介绍如何给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());
        }

    }

}

最新文章

  1. js 无线弹窗
  2. 基于cmake编译安装MySQL-5.5
  3. Python使用CGIHTTPServer调用shell作为cgi脚本
  4. Python字符串的修改以及传参
  5. 洛谷P1736 创意吃鱼法
  6. HTML5 datalist实现suggest功能
  7. Javascript 基础编程练习一
  8. hive regex insert join group cli
  9. EPROCESS KPROCESS PEB
  10. 【NOIP】OpenJudge - 15-03:雇佣兵
  11. Dynamics CRM 后台通过组织服务获取时间字段值的准确转换
  12. matlab导入txt数据画图
  13. $.each()和$().each(),以及forEach()的用法
  14. 如何修改maven的默认jdk版本
  15. 常用 Linux 命令使用说明
  16. Windows搭建Express环境
  17. Windows XP Professional产品序列号
  18. 江西财经大学第一届程序设计竞赛 G题 小Q的口袋校园
  19. 微信小程序-实现分享(带参数)
  20. centos6.5 + 7 静态ip配置

热门文章

  1. WinCE非通用调试工具汇总
  2. 简单了解ICMP协议
  3. 监控jvm的一个坑
  4. 关于java中final关键字与线程安全性
  5. HiKariCP的数据源配置
  6. 【腾讯GAD暑期训练营游戏程序班】游戏场景管理作业说明文档
  7. 利用百度API Store接口进行火车票查询
  8. [webpack] webpack-dev-server介绍及配置
  9. Prince2七大原则(4)
  10. 做参数可以读取参数 保存参数 用xml文件的方式