千家信息网

如何使用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"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0