如何使用Spring SpEL
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,本篇内容主要讲解"如何使用Spring SpEL",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何使用Spring SpEL"吧!SpEL Spring表
千家信息网最后更新 2026年11月03日如何使用Spring SpEL
本篇内容主要讲解"如何使用Spring SpEL",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何使用Spring SpEL"吧!
SpEL Spring表达式语言,在千人前面,根据不同用户不同属性匹配不同数据时很有用,以下简单几个实例显示怎么使用。
import com.google.common.collect.Lists;import com.google.common.collect.Sets;import org.springframework.expression.Expression;import org.springframework.expression.ExpressionParser;import org.springframework.expression.spel.standard.SpelExpressionParser;import org.springframework.expression.spel.support.StandardEvaluationContext;import java.util.List;import java.util.Set;public class Test1 { // 用户标签列表 private List userTagList; // 用户手机号列表 private List userMobileList; public void setUserTagList(List userTagList) { this.userTagList = userTagList; } public void setUserMobileList(List userMobileList) { this.userMobileList = userMobileList; } /** * 全部匹配 * 目标属性 全部在 用户属性 中,返回true * * @param targetTags 目标属性 * @return */ public boolean tagMatchAll(Integer... targetTags) { // 用户标签列表 Set userSet = Sets.newHashSet(userTagList); // 目标属性 Set targetSet = Sets.newHashSet(targetTags); // 复制目标属性,不能改变原有的属性值 Set targetSetCopy = Sets.newHashSet(targetSet); targetSetCopy.removeAll(userSet); return targetSetCopy.isEmpty(); } /** * 全部匹配 * * @param targetMobiles * @return */ public boolean mobileMatchAll(String... targetMobiles) { // 用户标签列表 Set userSet = Sets.newHashSet(userMobileList); // 目标属性 Set targetSet = Sets.newHashSet(targetMobiles); // 复制目标属性,不能改变原有的属性值 Set targetSetCopy = Sets.newHashSet(targetSet); targetSetCopy.retainAll(userSet); return !targetSetCopy.isEmpty(); } /** * 匹配到一个(交集不为空) 返回 true * * @param targetTags * @return */ public boolean tagMatchAny(Integer... targetTags) { // 用户标签列表 Set userTagSet = Sets.newHashSet(userTagList); // 目标属性 Set targetSet = Sets.newHashSet(targetTags); // 复制目标属性,不能改变原有的属性值 Set targetSetCopy = Sets.newHashSet(targetSet); targetSetCopy.retainAll(userTagSet); return !targetSetCopy.isEmpty(); } /** * 一个都没有匹配到(交集为空) 返回 true * * @param targetTags * @return */ public boolean tagMatchNotAny(Integer... targetTags) { return !this.tagMatchAny(targetTags); } public static void test() { List userTagList = Lists.newArrayList(10, 20, 30); List userMobileList = Lists.newArrayList("188"); Test1 scene = new Test1(); scene.setUserTagList(userTagList); scene.setUserMobileList(userMobileList); StandardEvaluationContext context = new StandardEvaluationContext(); context.setVariable("scene", scene); ExpressionParser parser = new SpelExpressionParser(); // 全部匹配 String sceneCondition1 = "#scene.tagMatchAll(10, 20) && #scene.mobileMatchAll('188')"; Expression expression1 = parser.parse_Expression(sceneCondition1); Boolean isTagMatchAll1 = expression1.getValue(context, Boolean.class); System.out.println("全部匹配1:" + isTagMatchAll1); // true String sceneCondition2 = "#scene.tagMatchAll(10, 20, 21)"; Expression expression2 = parser.parse_Expression(sceneCondition2); Boolean isTagMatchAll2 = expression2.getValue(context, Boolean.class); System.out.println("全部匹配2:" + isTagMatchAll2); // false:userTagList 中没有21 // 匹配任意一个 String sceneCondition3 = "#scene.tagMatchAny(10, 21, 31)"; Expression expression3 = parser.parse_Expression(sceneCondition3); Boolean isTagMatchAny3 = expression3.getValue(context, Boolean.class); System.out.println("匹配任意一个:" + isTagMatchAny3); // true 匹配到 10 // 完全不匹配 String sceneCondition4 = "#scene.tagMatchNotAny(11, 21, 31)"; Expression expression4 = parser.parse_Expression(sceneCondition4); Boolean isTagMatchNotAny4 = expression4.getValue(context, Boolean.class); System.out.println("完全不匹配:" + isTagMatchNotAny4); // true 匹配到 10 } public static void main(String[] args) { test(); }} 到此,相信大家对"如何使用Spring SpEL"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
属性
目标
用户
标签
不同
交集
内容
学习
实用
更深
兴趣
实例
实用性
实际
手机
手机号
操作简单
数据
方法
更多
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网络科技有限公司
互联网科技股票
九亚互联网科技
碳银互联网科技有限公司
互联网科技的未来发展
互联网科技论坛
互联网 新科技
互联网科技公司介绍
佰商互联网科技有限公司
天扬科技互联网广告
深圳卡尔迅互联网科技有限公司怎么样
西藏七维空间互联网科技有限公司
隆华科技工业互联网
互联网科技大赛获奖感言
有人说阿里系是互联网科技企业
互联网时代科技型公司的机会
米泉互联网科技专业怎么样
青岛互联网科技园
上海旺乐尼互联网科技有限公司
瑞安市蟠桃会互联网科技有限公司
高科技互联网
互联网科技媒体
亿策互联网科技
多迪互联网科技有限公司
云创客互联网科技公司
中国科技创新与世界互联网大会
畅思互联网科技
科技互联网海报
互联网营销推广黑科技
济南小医互联网科技有限公司招聘