当前位置: 首页 > news >正文

6.SpringEL与List,Map

SpringEL与List,Map

文章目录

  • SpringEL与List,Map
    • 介绍
    • Spring EL以注解的形式
    • Spring EL以XML的形式

介绍

使用SpEL与 Map 和 List 的工作方式与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,并添加了一些初始测试数据

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;}@Overridepublic String toString() {return "Customer [mapA=" + mapA + ", list=" + list + "]";}}
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;}}

执行程序

Customer obj = (Customer) context.getBean("customerBean");
System.out.println(obj);

输出结果:

Customer [mapA=This is MapA, list=List0]

Spring EL以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/beanshttp://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>
http://www.lryc.cn/news/167240.html

相关文章:

  • 【Oracle】使用 SQL Developer 连接 Oracle 数据库
  • PostgreSQL 事务并发锁
  • CANoe-Model Editor无法修改ARXML文件的问题、E2E在SOME/IP通信中的使用问题
  • Conan安装第三方依赖库时SSL验证失败解决办法
  • 基于springboot+vue的大学生智能消费记账系统
  • Java——》synchronized的使用
  • vue+element使用阿里的图标库保存图标
  • Day 01 web前端基础知识
  • Redis 高可用之持久化
  • 生成元 rust解法
  • 某ERP系统存在RCE漏洞
  • ElasticSearch 因为索引字段改变,平滑迁移索引
  • invalid use of incomplete type ‘class Ui::xxx‘
  • 变压器寿命预测(python代码,Logistic Regression模型预测效果一般,可以做对比实验)
  • 微信小程序-增加隐私协议弹窗
  • 分布式事务解决方案之可靠消息最终一致性
  • ROS学习笔记(四)---使用 VScode 启动launch文件运行多个节点
  • 编译Redis时报错: jemalloc/jemalloc.h: No such file or directory
  • LLM 05-大模型法律
  • 1-5 AUTOSAR数据交换文件ARXML
  • 学习尚硅谷HTML+CSS总结
  • 自己设计CPU学习之路——基于《Xilinx FPGA应用开发》
  • 数据结构与算法:树
  • Spark 【Spark SQL(一)DataFrame的创建、保存与基本操作】
  • 026-从零搭建微服务-文件服务(二)
  • Jenkins 页面部分显示Http状态403 被禁止
  • ajax day4
  • 8.Spring EL与ExpressionParser
  • Go和Java实现迭代器模式
  • 如何在 Vue.js 和 Nuxt.js 之间做出选择?