In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-10-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use Spring SpEL", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's learn how to use Spring SpEL!
SpEL Spring expression language, in front of thousands of people, is very useful when matching different data according to different attributes of different users. The following simple examples show how to use it.
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 { //User tag list private List userTagList; //List of user mobile phone numbers private List userMobileList; public void setUserTagList(List userTagList) { this.userTagList = userTagList; } public void setUserMobileList(List userMobileList) { this.userMobileList = userMobileList; } /** * all match * Target attributes are all in user attributes, return true * * @param targetTags Target Properties * @return */ public boolean tagMatchAll(Integer... targetTags) { //User tag list Set userSet = Sets.newHashSet(userTagList); //Target Properties Set targetSet = Sets.newHashSet(targetTags); //copy target attribute, original attribute value cannot be changed Set targetSetCopy = Sets.newHashSet(targetSet); targetSetCopy.removeAll(userSet); return targetSetCopy.isEmpty(); } /** * all match * * @param targetMobiles * @return */ public boolean mobileMatchAll(String... targetMobiles) { //User tag list Set userSet = Sets.newHashSet(userMobileList); //Target Properties Set targetSet = Sets.newHashSet(targetMobiles); //copy target attribute, original attribute value cannot be changed Set targetSetCopy = Sets.newHashSet(targetSet); targetSetCopy.retainAll(userSet); return ! targetSetCopy.isEmpty(); } /** * Match to one (intersection is not empty) Returns true * * @param targetTags * @return */ public boolean tagMatchAny(Integer... targetTags) { //User tag list Set userTagSet = Sets.newHashSet(userTagList); //Target Properties Set targetSet = Sets.newHashSet(targetTags); //copy target attribute, original attribute value cannot be changed Set targetSetCopy = Sets.newHashSet(targetSet); targetSetCopy.retainAll(userTagSet); return ! targetSetCopy.isEmpty(); } /** * None matched (intersection is empty) Returns 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(); //All matches 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("MatchAll 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("All Match 2:" + isTagMatchAll2); // false:userTagList does not contain 21 //match any one String sceneCondition3 = "#scene.tagMatchAny(10, 21, 31)"; Expression expression3 = parser.parse_Expression(sceneCondition3); Boolean isTagMatchAny3 = expression3.getValue(context, Boolean.class); System.out.println("Match any: " + isTagMatchAny3); // true matches to 10 //completely mismatched String sceneCondition4 = "#scene.tagMatchNotAny(11, 21, 31)"; Expression expression4 = parser.parse_Expression(sceneCondition4); Boolean isTagMatchNotAny4 = expression4.getValue(context, Boolean.class); System.out.println("Total Mismatch: " + isTagMatchNotAny4); // true matches to 10 } public static void main(String[] args) { test(); }} At this point, I believe that everyone has a deeper understanding of "how to use Spring SpEL", so you may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.