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]
相关推荐
雪落漂泊20 小时前
Liunux进程(七):程序控制篇(中)
linux·运维·windows
承渊政道1 天前
Fooocus本地AI绘图教程:Windows部署、低门槛生图与远程访问
人工智能·windows·cpolar·fooocus·生图
陈工大模型2 天前
收到乱码压缩包之后,我重新测了 7-Zip、Bandizip 和win解压缩
windows·电脑·软件工程·软件需求·7-zip
谙弆悕博士2 天前
在文件夹右键直接打开 Kali WSL:一条 `wsl --cd` 注册表脚本
windows·脚本·wsl·右击·ksli
陈工大模型2 天前
解压工具的三个技术分水岭:编码、分卷、容器格式
人工智能·windows·安全·解压工具·免费国内办公软件
Zucker n3 天前
文献发现、课题组织与证据可追溯的论文阅读。
论文阅读·windows·ubuntu
flyingonpine3 天前
PowerShell脚本数字签名实战:自签名证书、代码签名与信任链验证
windows·信任链
goehou4 天前
让 AI 替我操作这台电脑,先把手能伸多远定下来
windows·自动化·权限管理·ai agent
公子小六4 天前
基于.NET的Windows窗体编程之WinForms数据持久化(1)
windows·microsoft·c#·.net·winforms
2601_967987374 天前
安装 Redis 或 Redis Stack
linux·windows·redis·macos·安装