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]
相关推荐
paul_chen2114 小时前
解决Windows “此电脑”出现两个“图片”文件夹的问题
windows
SoulRoar.15 小时前
在 Windows 上面部署 StartLux
windows·大模型·startlux
爱吃提升15 小时前
DirectX修复工具安装配置教程-运行权限检测修复与运行库更新参数速查
windows
ddshub_cc17 小时前
Claude Code 接入 DDS Hub:Windows/macOS 配置、验证与报错排查
windows·macos·ai编程·claude·codex·ai模型·aiapi
第25小时记录17 小时前
[windows] windows下将文件夹映射为磁盘
windows
天空有片七色的云17 小时前
AutoTrigger:开源的 Windows 离线安装自动化工具(GUI+CLI,自动操作安装向导)
windows
2601_9679801119 小时前
Python之wxPython模块的安装
linux·windows·python·wxpython·gui设计·1024程序员节
启观川20 小时前
[windows] windows下将文件夹映射为磁盘
windows·笔记
鬼手点金1 天前
带工具调用的多轮交互:Agent 的核心循环
服务器·人工智能·windows·深度学习·microsoft·机器学习·交互
jiangliushi61 天前
GPT-6.1 Sol 免费用:Windows 装完 Codex 提示“不是内部或外部命令”?三步加好 PATH
windows·gpt