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. 【Latex】如何在Latex中插入伪代码 —— clrscode3e

    1. 简介clrscode3e是<算法导论(第三版)>使用的伪代码的宏包,clrs其实表示的是Cormen.Leiserson.Rivest和Stein.它有个更老的版本clrscode, ...

  2. 客户视角:Oracle ETL工具ODI

    客户视角:Oracle ETL工具ODI 数据集成已成为企业在追求市场份额中的关键技术组件,与依靠手工编码的方式不同,越来越多的企业选择完整的数据集成解决方案来支持其IT战略,从大数据分析到云平台的集 ...

  3. C++中关于指针初始化和使用NULL的理解

    1.严禁使用未被初始化的指针:C++创建指针的时候,只分配存储地址的内存,并不会分配存储数据的内存,所以指针可能指向任何位置. (1)使用解除运算符(*)之前,一定要对指针初始化,否则若声明的指针刚好 ...

  4. grunt + compass retina sprites

    https://github.com/AdamBrodzinski/Retina-Sprites-for-Compass

  5. JPA和Hibernate的区别

    JPA Java Persistence API,是Java EE 5的标准ORM接口,也是ejb3规范的一部分. Hibernate,当今很流行的ORM框架,是JPA的一个实现,但是其功能是JPA的 ...

  6. C# 创建iis站点以及IIS站点属性,iis不能启动站点

    DontLog = False是否将客户端的请求写入日志文件 2011年04月09日 #region CreateWebsite 新增网站 public string CreateWebSite(st ...

  7. LA 3704 (矩阵快速幂 循环矩阵) Cellular Automaton

    将这n个格子看做一个向量,每次操作都是一次线性组合,即vn+1 = Avn,所求答案为Akv0 A是一个n*n的矩阵,比如当n=5,d=1的时候: 不难发现,A是个循环矩阵,也就是将某一行所有元素统一 ...

  8. (任寒韬)WebApp群主 - MobileTech 资料

    web app : http://www.lightapp.cn/brand/index/4101 https://github.com/jtyjty99999/mobileTech/blob/mas ...

  9. 解决魅族USB调试无法被电脑识别的问题(含Mac OS X、Win7)

      每次打开豌豆荚或者360手机助手之类手机助手后Eclipse才会检测到mx4(实际上是豌豆荚关闭eclipse的adb使用自己的驱动连接的).解决方法就是在"adb_usb.ini&qu ...

  10. LeetCode Number of Islands 岛的数量(DFS,BFS)

    题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛? 思路:DFS还是比较短,实现了一下.如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的. class S ...