6.SpringEL与List,Map

SpringEL与List,Map

文章目录

  • SpringEL与List,Map
    • 介绍
    • [Spring EL以注解的形式](#Spring EL以注解的形式)
    • [Spring EL以XML的形式](#Spring EL以XML的形式)

介绍

使用SpEL与 Map 和 List 的工作方式与Java是完全一样的

java 复制代码
//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以注解的形式

在这里,创建了一个HashMap和ArrayList,并添加了一些初始测试数据

java 复制代码
package com.yiibai.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 + "]";
	}

}
java 复制代码
package com.yiibai.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 MapA");
		map.put("MapB", "This is MapB");
		map.put("MapC", "This is MapC");

		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;
	}

}

执行程序

java 复制代码
Customer obj = (Customer) context.getBean("customerBean");
System.out.println(obj);

输出结果:

复制代码
Customer [mapA=This is MapA, list=List0]

Spring EL以XML的形式

xml 复制代码
<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.yiibai.core.Customer">
		<property name="mapA" value="#{testBean.map['MapA']}" />
		<property name="list" value="#{testBean.list[0]}" />
	</bean>

	<bean id="testBean" class="com.yiibai.core.Test" />

</beans>
相关推荐
lcj25111 小时前
【C语言】数据在内存中的存储
c语言·数据结构
旖-旎1 小时前
哈希表(字母异位次分组)(5)
数据结构·c++·算法·leetcode·哈希算法·散列表
0xDevNull1 小时前
Windows系统使用nvm实现多版本切换Node.js详细教程
windows·node.js
无限进步_1 小时前
【C++】多重继承中的虚表布局分析:D类对象为何有两个虚表?
开发语言·c++·ide·windows·git·算法·visual studio
学Linux的语莫2 小时前
Hyper-V的安装使用
linux·windows·ubuntu·hyper-v
paeamecium2 小时前
【PAT甲级真题】- All Roads Lead to Rome (30)
数据结构·c++·算法·pat考试·pat
PD我是你的真爱粉3 小时前
Redis 数据类型与底层实现:从 SDS、Quicklist 到 ZSet 跳表彻底讲透
数据结构·redis
xiaoshuaishuai83 小时前
C# 方言识别
开发语言·windows·c#
汀、人工智能3 小时前
[特殊字符] 第100课:任务调度器
数据结构·算法·数据库架构·贪心··任务调度器
会编程的土豆3 小时前
日常做题 vlog
数据结构·c++·算法