zoukankan      html  css  js  c++  java
  • Spring EL Lists, Maps example

    In this article, we show you how to use Spring EL to get value from Map and List. Actually, the way of SpEL works with Map and List is exactly same with Java. See example :

            //get map whete key = 'MapA'
    	@Value("#{testBean.map['MapA']}")
    	private String mapA;
    
    	//get first value from list, list is 0-based.
    	@Value("#{testBean.list[0]}")
    	private String list;
    

    Spring EL in Annotation

    Here, created a HashMap and ArrayList, with some initial data for testing.

    package com.mkyong.core;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component("customerBean")
    public class Customer {
    
    	@Value("#{testBean.map['MapA']}")
    	private String mapA;
    
    	@Value("#{testBean.list[0]}")
    	private String list;
    
    	public String getMapA() {
    		return mapA;
    	}
    
    	public void setMapA(String mapA) {
    		this.mapA = mapA;
    	}
    
    	public String getList() {
    		return list;
    	}
    
    	public void setList(String list) {
    		this.list = list;
    	}
    
    	@Override
    	public String toString() {
    		return "Customer [mapA=" + mapA + ", list=" + list + "]";
    	}
    
    }
    
    package com.mkyong.core;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import org.springframework.stereotype.Component;
    
    @Component("testBean")
    public class Test {
    
    	private Map<String, String> map;
    	private List<String> list;
    
    	public Test() {
    		map = new HashMap<String, String>();
    		map.put("MapA", "This is A");
    		map.put("MapB", "This is B");
    		map.put("MapC", "This is C");
    
    		list = new ArrayList<String>();
    		list.add("List0");
    		list.add("List1");
    		list.add("List2");
    
    	}
    
    	public Map<String, String> getMap() {
    		return map;
    	}
    
    	public void setMap(Map<String, String> map) {
    		this.map = map;
    	}
    
    	public List<String> getList() {
    		return list;
    	}
    
    	public void setList(List<String> list) {
    		this.list = list;
    	}
    
    }
    

    Run it

           Customer obj = (Customer) context.getBean("customerBean");
           System.out.println(obj);
    

    Output

    Customer [mapA=This is A, list=List0]
    

    Spring EL in XML

    See equivalent version in bean definition XML file.

    <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-3.0.xsd">
    
    	<bean id="customerBean" class="com.mkyong.core.Customer">
    		<property name="mapA" value="#{testBean.map['MapA']}" />
    		<property name="list" value="#{testBean.list[0]}" />
    	</bean>
    
    	<bean id="testBean" class="com.mkyong.core.Test" />
    
    </beans>
    
  • 相关阅读:
    creat-react-app/dva静态项目,用nginx部署在次级域名路径(如a.com/sub/)需要注意的几点
    如何在 Rails 中搭配 Turbolinks 使用 Vue
    绝对干货!漫谈美团APP对Crash的治理之路
    下载更省心!12月这些应用获得了绿色应用认证!
    代码之外的生存指南,这6本书助你提升软实力
    “社交通讯类”Target SDK≥26优秀应用展示
    “实用工具类”Target SDK≥26优秀应用展示
    “购物比价类”Target SDK≥26优秀应用展示
    知否知否!应用市场上架Target SDK新规来袭
    大咖推荐!今年值得一读的6本技术类书籍
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4749608.html
Copyright © 2011-2022 走看看