本篇内容主要讲解“如何使用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<Integer> userTagList;
// 用户手机号列表
private List<String> userMobileList;
public void setUserTagList(List<Integer> userTagList) {
this.userTagList = userTagList;
}
public void setUserMobileList(List<String> userMobileList) {
this.userMobileList = userMobileList;
}
/**
* 全部匹配
* 目标属性 全部在 用户属性 中,返回true
*
* @param targetTags 目标属性
* @return
*/
public boolean tagMatchAll(Integer... targetTags) {
// 用户标签列表
Set<Integer> userSet = Sets.newHashSet(userTagList);
// 目标属性
Set<Integer> targetSet = Sets.newHashSet(targetTags);
// 复制目标属性,不能改变原有的属性值
Set<Integer> targetSetCopy = Sets.newHashSet(targetSet);
targetSetCopy.removeAll(userSet);
return targetSetCopy.isEmpty();
}
/**
* 全部匹配
*
* @param targetMobiles
* @return
*/
public boolean mobileMatchAll(String... targetMobiles) {
// 用户标签列表
Set<String> userSet = Sets.newHashSet(userMobileList);
// 目标属性
Set<String> targetSet = Sets.newHashSet(targetMobiles);
// 复制目标属性,不能改变原有的属性值
Set<String> targetSetCopy = Sets.newHashSet(targetSet);
targetSetCopy.retainAll(userSet);
return !targetSetCopy.isEmpty();
}
/**
* 匹配到一个(交集不为空) 返回 true
*
* @param targetTags
* @return
*/
public boolean tagMatchAny(Integer... targetTags) {
// 用户标签列表
Set<Integer> userTagSet = Sets.newHashSet(userTagList);
// 目标属性
Set<Integer> targetSet = Sets.newHashSet(targetTags);
// 复制目标属性,不能改变原有的属性值
Set<Integer> 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<Integer> userTagList = Lists.newArrayList(10, 20, 30);
List<String> 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”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/3777515/blog/5017796