博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring EL Lists, Maps example
阅读量:5960 次
发布时间:2019-06-19

本文共 2271 字,大约阅读时间需要 7 分钟。

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
map; private List
list; public Test() { map = new HashMap
(); map.put("MapA", "This is A"); map.put("MapB", "This is B"); map.put("MapC", "This is C"); list = new ArrayList
(); list.add("List0"); list.add("List1"); list.add("List2"); } public Map
getMap() { return map; } public void setMap(Map
map) { this.map = map; } public List
getList() { return list; } public void setList(List
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.

转载于:https://www.cnblogs.com/ghgyj/p/4749608.html

你可能感兴趣的文章
expdp 详解及实例
查看>>
解读最具O2O属性—哈根达斯微信企业号的成功之道
查看>>
Extjs4.x (MVC)Controller中refs以及Ext.ComponentQuery解析
查看>>
Server-01 How to Find the Remote Desktop Port
查看>>
Java--接口、抽象与继承
查看>>
通过IP判断登录地址
查看>>
Oracle闪回技术
查看>>
利用单壁路由实现vlan间路由
查看>>
hello world
查看>>
CentOS 7 配置yum本地base源和阿里云epel源
查看>>
python 学习导图
查看>>
生成树
查看>>
深入浅出JavaScript (五) 详解Document.write()方法
查看>>
用XSLT和XML改进Struts
查看>>
Beta冲刺——day6
查看>>
Comet OJ - Contest #3 题解
查看>>
[网络流24题-9]试题库问题
查看>>
jquery选择器详解
查看>>
C# 保留2位小数
查看>>
使用xshell远程连接Linux
查看>>