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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 14,593评论 5 6
  • 记不清有多久没有写东西了,一直以来很喜欢欣赏美文,自以为由工作忙没时间,而不断使自己惰性增加,今夜终于不用加班...
    萱萱美女妈阅读 4,233评论 1 2
  • “大圣,你真的是齐天大圣吗?” “你看,我这还有一个齐天大圣哦,送给你好不好?” “有他保护你,你就什么都不用怕了...
    万古如斯阅读 2,299评论 1 1
  • 前言:孩子们开始养蚕,我同步重养天虫。就把近二十年前的旧文发上来吧。 我始终不能忘却夕阳下那个长长的、孤独的身影,...
    干国祥备课老干阅读 5,872评论 10 9
  • 十七岁,最纯最美的年纪, 十七岁,最傻最无忧的时期, 时隔多年,总会有一抹触动撩拨你的心弦,耳语寄你:亲,是否记得...
    limon日记阅读 3,590评论 0 0