本文已收录在Github,关注我,紧跟本系列专栏文章,咱们下篇再续!
- 🚀 魔都架构师 | 全网30W技术追随者
- 🔧 大厂分布式系统/数据中台实战专家
- 🏆 主导交易系统百万级流量调优 & 车联网平台架构
- 🧠 AIGC应用开发先行者 | 区块链落地实践者
- 🌍 以技术驱动创新,我们的征途是改变世界!
- 👉 实战干货:编程严选网
1 相关类
- org.springframework.expression.spel.standard.SpelExpressionParser,解析SPEL表达式
- org.springframework.expression.spel.support.StandardEvaluationContext,验证方法名是否符合表达式
2 示例
public class MethodNameEvaluator { // 判断方法名是否符合给定的SPEL表达式 public static boolean isMatch(String methodName, String spelExpression) { SpelExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); context.setVariable("methodName", methodName); return parser.parseExpression(spelExpression).getValue(context, Boolean.class); } public static void main(String[] args) { String methodName = "getUserById"; // 匹配"get"开头,"Id"结尾方法名 String spelExpression = "#methodName.matches('get.*ById')"; boolean isMatched = isMatch(methodName, spelExpression); // true System.out.println(isMatched); } }
- 先用SpelExpressionParser解析表达式
- 再创建一个StandardEvaluationContext对象,并将方法名作为变量设置到上下文中
- 最后,用parseExpression解析表达式,并用getValue获取表达式的结果