3.SpringEL方法调用实例

SpringEL方法调用实例

文章目录

介绍

Spring表达式语言(使用SpEL)允许开发人员使用表达式来执行方法和将返回值以注入的方式到属性,或叫作"使用SpEL方法调用"

Spring EL在注解的形式

了解如何实现Spring EL方法调用与@Value注释

java 复制代码
package com.yiibai.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("customerBean")
public class Customer {

	@Value("#{'yiibai'.toUpperCase()}")
	private String name;

	@Value("#{priceBean.getSpecialPrice()}")
	private double amount;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getAmount() {
		return amount;
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}

	@Override
	public String toString() {
		return "Customer [name=" + name + ", amount=" + amount + "]";
	}

}
java 复制代码
package com.yiibai.core;

import org.springframework.stereotype.Component;

@Component("priceBean")
public class Price {

	public double getSpecialPrice() {
		return new Double(199.09);
	}

}

输出

复制代码
Customer [name=YIIBAI, amount=199.09]

在字串文本上调用 toUpperCase()方法

java 复制代码
@Value("#{'yiibai'.toUpperCase()}")
private String name;

在 'priceBean',Bean上调用getSpecialPrice() 方法

java 复制代码
@Value("#{priceBean.getSpecialPrice()}")
private double amount;

SpringEL调用List,Map中的方法

从List中过滤元素

假设你有一个包含各种字符串的List,你想使用SpEL来过滤出包含特定字符的元素。你可以使用contains方法来实现这个功能。

java 复制代码
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {
    private List<String> myList = Arrays.asList("apple", "banana", "cherry", "date");

    public void filterList(String searchTerm) {
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression("myList[#root.contains(value, 'searchTerm')]");
        List<String> filteredList = (List<String>) exp.getValue(myList, Object[].class);
        System.out.println(filteredList);  // prints: [banana, cherry]
    }
}

在这个例子中,我们使用了SpEL的contains函数来检查列表中的每个元素是否包含搜索词。如果是,那么这个元素就会被包含在结果列表中

从Map中获取值

假设你有一个包含键值对的Map,你想使用SpEL来获取特定键的值。你可以直接使用Map的键作为SpEL表达式的一部分来获取值。

java 复制代码
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {
    private Map<String, Integer> myMap = new HashMap<>();
    myMap.put("apple", 1);
    myMap.put("banana", 2);
    myMap.put("cherry", 3);
    myMap.put("date", 4);

    public void getValueFromMap(String key) {
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression("myMap[key]");
        Integer value = (Integer) exp.getValue(myMap, Object.class);
        System.out.println(value);  // prints: 2
    }
}

在这个例子中,我们使用了SpEL来获取Map中特定键的值。我们直接在SpEL表达式中使用键名作为索引来访问Map的值。

注意:这些示例需要在Spring框架的环境中运行,因为SpEL是Spring的一部分。

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="name" value="#{'yiibai'.toUpperCase()}" />
		<property name="amount" value="#{priceBean.getSpecialPrice()}" />
	</bean>
 
	<bean id="priceBean" class="com.yiibai.core.Price" />
	
</beans>

输出

复制代码
Customer [name=YIIBAI, amount=199.09]
相关推荐
Lxinccode1 小时前
python(55) : python程序设置为Windows快捷方式
windows·python固定到开始·快速调用开始
纳于大麓3 小时前
Kotlin基础语法
linux·windows·kotlin
呉師傅7 小时前
关于联想ThinkCentre M950t-N000 M大师电脑恢复预装系统镜像遇到的一点问题
运维·网络·windows·电脑
网安INF9 小时前
Python核心数据结构与函数编程
数据结构·windows·python·网络安全
知识分享小能手12 小时前
uni-app 入门学习教程,从入门到精通,uni-app 基础知识详解 (2)
前端·javascript·windows·学习·微信小程序·小程序·uni-app
waving-black13 小时前
windows系统下安装测试kafka
windows·分布式·kafka
傻啦嘿哟16 小时前
Python 高效清理 Excel 空白行列:从原理到实战
windows
晋人在秦 老K16 小时前
鼠标连点效率低?MouseClickTool 2.0三步配置,自动化操作提速80% 重复点击太耗时?Windows鼠标连点器实操教程,开发测试效率翻倍
windows·自动化·计算机外设·鼠标连点器·gui模拟点击·自动化点击解决方案
shykevin16 小时前
uni-app x商城,商品列表组件封装以及使用
windows·uni-app
CHH32131 天前
在 Mac/linux 的 VSCode 中使用Remote-SSH远程连接 Windows
linux·windows·vscode·macos