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>

Spring EL Lists, Maps example的更多相关文章

  1. 把功能强大的Spring EL表达式应用在.net平台

    Spring EL 表达式是什么? Spring3中引入了Spring表达式语言—SpringEL,SpEL是一种强大,简洁的装配Bean的方式,他可以通过运行期间执行的表达式将值装配到我们的属性或构 ...

  2. Spring3系列6 - Spring 表达式语言(Spring EL)

    Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...

  3. Test Spring el with ExpressionParser

    Spring expression language (SpEL) supports many functionality, and you can test those expression fea ...

  4. Spring EL regular expression example

    Spring EL supports regular expression using a simple keyword "matches", which is really aw ...

  5. Spring EL ternary operator (if-then-else) example

    Spring EL supports ternary operator , perform "if then else" conditional checking. For exa ...

  6. Spring EL Operators example

    Spring EL supports most of the standard mathematical, logical or relational operators. For example, ...

  7. Spring EL method invocation example

    In Spring EL, you can reference a bean, and nested properties using a 'dot (.)' symbol. For example, ...

  8. Spring EL hello world example

    The Spring EL is similar with OGNL and JSF EL, and evaluated or executed during the bean creation ti ...

  9. Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)

    一.Bean的Scope Scope描述的是Spring容器如何新建Bean实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只 ...

随机推荐

  1. WinCE启动次数的记录

    最近一周一直在忙于测试NAND文件系统的稳定性和可靠性,今天终于有所进展.测试组所有同事齐上阵,加上小高和我,测试了一天,都未发现问题.虽然还不能保证完全OK,但至少有所改善了. 测试组今天主要做了文 ...

  2. vi 编辑内容中查找字符位置

    [root@localhost gdm]# vi /etc/X11/gdm/gdm.conf # You can also use the gdm-restart and gdm-safe-resta ...

  3. Android studio编译之后显示中文乱码的问题解决办法

    在build.gradle文件中加上 android {compileOptions.encoding = "GBK"}

  4. IE css expression(表达式)

    很多时候我们需要对IE6的bug写一些hack,如max-height,absolute元素高度100%等. css里面的 expression(表达式)和js里面的差不多,如: 获取当前元素的高度: ...

  5. JSOI2015 分组赛记

    分组赛结束了,虽然跟我关系不大,但是去了还是学到了不少东西 day1 上午报到,在宾馆遇到大神wzy,orz 好像没有参赛证发了,于是给我发了一个[工作证],233我是工作人员了,高贵冷艳 下午是常中 ...

  6. 利用序列化的方式实现C#深复制和浅复制

    代码如下:具体看注释 [Serializable] public class A { public virtual string Name { get; set; } public int Age { ...

  7. 【iOS-cocos2d-X 游戏开发之九】Cocos2dx利用CCSAXParser解析xml数据&CCMutableDictionary使用与注意!

    本站文章均为李华明Himi原创,转载务必在明显处注明:转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-cocos2dx/694.html ☞ ...

  8. 结合daterangepicker实现Datatables表格带参数查询

    http://dt.thxopen.com/example/user_share/send_extra_param.html#@一颗树 http://www.guoxk.com/node/jquery ...

  9. php复制目录及文件

    <?php /* 复制目录 */ function copydir($dirsrc,$dirto){ if(is_file($dirto)){ echo "目标不是目录不能创建&quo ...

  10. 对于GLM的理解,与方差分析的对比

    最近遇到一个问题,如果因变量为一个连续变量(如胰岛素水平),主要考察的变量为分组变量(如正常血糖组,前糖尿病组,糖尿病组三组),现在的目的是想看调整多种变量(包括多个连续性变量和分类变量)后,胰岛素水 ...