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]
相关推荐
萑澈7 小时前
Windows 7 运行 Electron 安装包报“不是有效的 Win32 应用程序”怎么办
javascript·windows·electron
overmind9 小时前
oeasy Python 121[专业选修]列表_多维列表运算_列表相加_列表相乘
java·windows·python
Vanranrr10 小时前
Windows 环境下 SVN 命令行能力补齐:一次工程化排障与标准化实践
windows·svn
xiaotao13111 小时前
01-编程基础与数学基石: Python核心数据结构完全指南
数据结构·人工智能·windows·python
m0_7381207212 小时前
渗透测试基础ctfshow——Web应用安全与防护(五)
前端·网络·数据库·windows·python·sql·安全
路由侠内网穿透.13 小时前
本地部署开源客服系统 FreeScout 并实现外部访问( Windows 版本)
运维·服务器·网络·windows·网络协议
zhangzeyuaaa14 小时前
从Windows到Linux:Shell脚本“无法执行:找不到需要的文件”问题深度解析与根治方案
linux·运维·windows
oh LAN16 小时前
Windows 下 Redis 开机自启
数据库·windows·redis
Warren9817 小时前
Windows本地部署n8n完整教程(基于Docker,新手友好)
运维·windows·python·测试工具·docker·容器·可用性测试
学安全的小白19 小时前
VMware安装windows server 2003 R2 教程(图文版)
服务器·windows