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>
相关推荐
炸膛坦客34 分钟前
嵌入式 - 数据结构与算法:(1-1)数据结构 - 顺序表(Sequential List)
数据结构·算法·嵌入式
水龙吟啸43 分钟前
数据结构与算法随机复习–Day1
数据结构·c++·算法
萧行之2 小时前
Ubuntu+Windows双系统:解决GRUB不显示Windows启动项、一闪而过问题
linux·windows·ubuntu
深念Y3 小时前
踩坑实录:把 Windows 默认 PowerShell 换成 7.x 到底有多坑?一条龙解决指南
windows·乱码·bug·控制台·powershell·管道·流式
夜猫逐梦3 小时前
[开发经验] DLL注入中控制台窗口无法关闭的排查与修复
c++·windows·控制台
无限进步_3 小时前
C++ 多态机制完全解析:从虚函数重写到动态绑定原理
java·c语言·jvm·数据结构·c++·windows·后端
天都3573 小时前
青少年ctf 日志排查 复盘
windows·网络安全·应急响应
无限进步_6 小时前
二叉搜索树完全解析:从概念到实现与应用场景
c语言·开发语言·数据结构·c++·算法·github·visual studio
南汁bbj7 小时前
彻底解决!Milvus远程连接报错code=2、gRPC超时问题(Windows访问Linux服务终极方案)
linux·windows·milvus
爱写代码的倒霉蛋7 小时前
2022年天梯赛L1-8真题解析(哈希+排序)
数据结构·算法