zoukankan      html  css  js  c++  java
  • 使用SpringEL操作List和Map集合

    原文链接:http://www.yiidian.com/spring/spring-el-bean.html

    这个例子演示SpEL表达式怎样从List、Map集合中取值,示例如下:

    一、编写Bean类

    TestCollection类,用于创建Map和List对象

    package com.yiidian.collection;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    /**
     * 
     * @author http://www.yiidian.com
     *
     */
    public class TestCollection {
    	private Map<String, String> nameMap;
    	private List<String> teleList;
    
    	public TestCollection() {
    		nameMap = new HashMap<String, String>();
    		nameMap.put("001", "张三");
    		nameMap.put("002", "李四");
    		nameMap.put("003", "王五");
    
    		teleList = new ArrayList<String>();
    		teleList.add("13422223333");
    		teleList.add("13544446666");
    		teleList.add("13788889999");
    
    	}
    
    	public Map<String, String> getNameMap() {
    		return nameMap;
    	}
    
    	public void setNameMap(Map<String, String> nameMap) {
    		this.nameMap = nameMap;
    	}
    
    	public List<String> getTeleList() {
    		return teleList;
    	}
    
    	public void setTeleList(List<String> teleList) {
    		this.teleList = teleList;
    	}
    	
    }
    

    Customer类:

    package com.yiidian.domain;
    
    import java.io.Serializable;
    /**
     * 
     * @author http://www.yiidian.com
     *
     */
    public class Customer implements Serializable{
    	private String name;
    	private String telephone;
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getTelephone() {
    		return telephone;
    	}
    	public void setTelephone(String telephone) {
    		this.telephone = telephone;
    	}
    	@Override
    	public String toString() {
    		return "Customer [name=" + name + ", telephone=" + telephone + "]";
    	}
    	
    }
    

    二、配置applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:p="http://www.springframework.org/schema/p"
        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="testCollection" class="com.yiidian.collection.TestCollection"></bean>
    
    	<bean id="customer" class="com.yiidian.domain.Customer">
    		<property name="name" value="#{testCollection.nameMap['001']}"/>
    		<property name="telephone" value="#{testCollection.teleList[0]}"/>
    	</bean>
    
    	 
    </beans>
    

    三、编写测试类

    package com.yiidian.test;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.yiidian.domain.Customer;
    
    /**
     * @author http://www.yiidian.com
     * 
     */
    public class Demo1 {
    
    	@Test
    	public void test1() {
    		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    		Customer customer = (Customer)context.getBean("customer");
    		System.out.println(customer);
    	}
    
    }
    

    四、运行结果

    file

    源码下载:http://pan.baidu.com/s/1hsCK0S0

    file

    欢迎关注我的公众号::一点教程。获得独家整理的学习资源和日常干货推送。
    如果您对我的系列教程感兴趣,也可以关注我的网站:yiidian.com

  • 相关阅读:
    Java快速开发框架LML功能菜单管理
    性能测试知多少性能需求分析
    C++学习的方法以及四大名著
    .NET Micro Framework官方库+Controller Area Network
    PostgreSQL学习笔记
    幻风阁|kent.zhu'sBlog对google阅读器共享设计的再讨论
    【数据库事务日志碎片原理分析与方案】分析篇
    Windows 8 RC升级到RTM很顺利
    http://www.cnblogs.com/oneday/archive/2012/08/16/2643039.html
    Visual Studio 2012 Ultimate RTM 体验
  • 原文地址:https://www.cnblogs.com/yiidian/p/12465633.html
Copyright © 2011-2022 走看看