本文共 4255 字,大约阅读时间需要 14 分钟。
直接看代码
1 基本类型解析
public class LiteralExprSample { /** * 对基本类型解析 * @param args */ public static void main(String[] args){ ExpressionParser parser=new SpelExpressionParser(); //解析字符串 //"'Hello world'" 或"\"Hello World\"" String helloWorld=(String)parser.parseExpression("\"Hello World\"").getValue(); String helloWorld1=(String)parser.parseExpression("'Hello World'").getValue(); //解析双精度浮点型 double doubleNumber=(Double)parser.parseExpression("6.0221415E+23").getValue(); //解析整型 int maxValue=(Integer)parser.parseExpression("0x7FFFFFFF").getValue(); //解析布尔型 boolean trueValue=(Boolean)parser.parseExpression("true").getValue(); //解析空值 Object nullValue=parser.parseExpression("null").getValue(); System.out.println(helloWorld); System.out.println(helloWorld1); System.out.println(doubleNumber); System.out.println(maxValue); System.out.println(trueValue); System.out.println(nullValue); }}Hello World Hello World 6.0221415E23 2147483647 true null
2对象属性解析
public class PropertyExprSample { /** * * 在Sqel中,可使用类似xxx.yyy.zzz的对象属性路径轻松地访问对象属性值。 * @param args */ public static void main(String[] args){ User user=new User(); user.setUserName("tom"); user.setLastVisit(new Date()); user.setCredits(100); user.setPlaceOfBirth(new PlaceOfBirth("中国","厦门")); //构造Spel解析上下文 ExpressionParser parser=new SpelExpressionParser(); EvaluationContext context=new StandardEvaluationContext(user); //基本属性值获取 String username=(String)parser.parseExpression("userName").getValue(context); int credits=(Integer)parser.parseExpression("credits+10").getValue(context); String city=(String)parser.parseExpression("placeOfBirth.city").getValue(context); System.out.println(username); System.out.println(credits); System.out.println(city); }}tom 110 厦门
3数组、集合类型解析
public class CollectionExprSample { /** * 在Sqel中支持数组、集合类型(map,list)的解析,数组支持标准创建数组的方式,如“new int[]{1,2,3}".不支持多元数组 * list支持大括号括起来的内容,如"{1,2,3,4}" "{ {'a','b'},{'x','y'}}". * map采用如下方式:{userName:'tom',credits:100} * @param args */ public static void main(String[] args){ User user=new User(); user.setUserName("tom"); user.setLastVisit(new Date()); user.setCredits(100); user.setPlaceOfBirth(new PlaceOfBirth("中国","厦门")); ExpressionParser parser=new SpelExpressionParser(); EvaluationContext context=new StandardEvaluationContext(user); //数组表达式解析 int[] array1=(int[])parser.parseExpression("new int[]{1,2,3}").getValue(context); int[][] array2=(int[][])parser.parseExpression("new int[2][3]").getValue(context); System.out.println(array1.length); System.out.println(array2.length); //目前不支持多维数组初始化,一下语句将报错 //int[][] array3=(int[][])parser.parseExpression("new int[2][3]{ {1,2,3},{4,5,6}}").getValue(context); //System.out.println(array3); //list表达式解析 List list=(List)parser.parseExpression("{1,2,3,4}").getValue(context); List listOfLists=(List)parser.parseExpression("{ {'a','b'},{'x','y'}}").getValue(context); System.out.println(list); System.out.println(listOfLists); //列表字符串解析 Map userInfo=(Map)parser.parseExpression("{userName:'tom',credits:100}").getValue(context); List userInfo2=(List)parser.parseExpression("{ {userName:'tom',credits:100},{userName:'tom',credits:100}}").getValue(context); System.out.println(userInfo); System.out.println(userInfo2); //从数组,list,map中取值 String interest1=(String)parser.parseExpression("interestsArray[0]").getValue(context); String interest2=(String)parser.parseExpression("interestsList[0]").getValue(context); String interest3=(String)parser.parseExpression("interestsMap['interest1']").getValue(context); System.out.println(interest1); System.out.println(interest2); System.out.println(interest3); }}3 2 [1, 2, 3, 4] [[a, b], [x, y]] {userName=tom, credits=100} [{userName=tom, credits=100}, {userName=tom, credits=100}] Hello Hello Hello
转载地址:http://qojqi.baihongyu.com/