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>
相关推荐
不穿格子的程序员1 天前
从零开始写算法——链表篇4:删除链表的倒数第 N 个结点 + 两两交换链表中的节点
数据结构·算法·链表
dragoooon341 天前
[hot100 NO.19~24]
数据结构·算法
电子硬件笔记1 天前
Python语言编程导论第七章 数据结构
开发语言·数据结构·python
Tony_yitao1 天前
15.华为OD机考 - 执行任务赚积分
数据结构·算法·华为od·algorithm
C雨后彩虹1 天前
任务总执行时长
java·数据结构·算法·华为·面试
柒.梧.1 天前
数据结构:二叉排序树构建与遍历的解析与代码实现
java·开发语言·数据结构
zhuzewennamoamtf1 天前
Linux内核platform抽象、数据结构、内核匹配机制
linux·运维·数据结构
自然常数e2 天前
深入理解指针(6)
c语言·数据结构·算法·visual studio
一杯美式 no sugar2 天前
数据结构——栈
c语言·数据结构·
蒙奇D索大2 天前
【数据结构】考研408 | 冲突解决精讲: 拉链法——链式存储的艺术与优化
数据结构·笔记·考研·改行学it