重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

如何使用SpringSpEL

本篇内容主要讲解“如何使用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.parseExpression(sceneCondition1);
        Boolean isTagMatchAll1 = expression1.getValue(context, Boolean.class);
        System.out.println("全部匹配1:" + isTagMatchAll1); // true

        String sceneCondition2 = "#scene.tagMatchAll(10, 20, 21)";
        Expression expression2 = parser.parseExpression(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.parseExpression(sceneCondition3);
        Boolean isTagMatchAny3 = expression3.getValue(context, Boolean.class);
        System.out.println("匹配任意一个:" + isTagMatchAny3); // true 匹配到 10

        // 完全不匹配
        String sceneCondition4 = "#scene.tagMatchNotAny(11, 21, 31)";
        Expression expression4 = parser.parseExpression(sceneCondition4);
        Boolean isTagMatchNotAny4 = expression4.getValue(context, Boolean.class);
        System.out.println("完全不匹配:" + isTagMatchNotAny4); // true 匹配到 10
    }

    public static void main(String[] args) {
        test();
    }

}

到此,相信大家对“如何使用Spring SpEL”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


新闻名称:如何使用SpringSpEL
链接分享:http://cqcxhl.com/article/piospp.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP