如何使用Spring SpEL
发表于:2025-11-15 作者:千家信息网编辑
千家信息网最后更新 2025年11月15日,本篇内容主要讲解"如何使用Spring SpEL",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何使用Spring SpEL"吧!SpEL Spring表
千家信息网最后更新 2025年11月15日如何使用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安全错误
数据库的锁怎样保障安全
计算机网络技术能专升本
vs软件开发的优点
数据库的学习多久能熟练掌握
怎么申请免费永久阿里云服务器
csgo平台服务器是什么
迁西网络技术质量保证
小学生网络安全卡片设计图片
计算机网络技术对应的岗位
网络安全的作文开头排比
魔兽轮回服服务器人数
梦幻花园服务器不同步
广州直播软件开发项目交流
上位机怎样开发软件开发
武汉市网络安全职业技术学院
江苏高性能服务器高性价比服务器
寝室网络安全会议
天津极客网络技术公司
串口怎么监视数据库
2016数据库期末答案
网络安全app反诈
软件开发 能力差的下属
5g软件开发靠谱吗
网络安全微党课ppt
服务器防护盾多久可以安装好
中兴软件开发岗深圳工作环境
期权软件开发哪家好
亿家安互联网科技有限公司电话
服务器删除iis
微擎模块安装后无法写入数据库
软件开发培训班有哪些条件