博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SqEl基础2
阅读量:4228 次
发布时间:2019-05-26

本文共 10567 字,大约阅读时间需要 35 分钟。

方法解析:

public class MethodExprSample {    public static void main(String[] args){        User user =new User();        ExpressionParser parser=new SpelExpressionParser();        EvaluationContext context=new StandardEvaluationContext(user);        //调用String方法        String substring=parser.parseExpression("'spring SpEL'.substring(7)").getValue(String.class);        Integer index=parser.parseExpression("'spring SqEL'.indexOf('SqEL')").getValue(Integer.class);        //调用实例方法        boolean isCorrect=parser.parseExpression("validatePassword('123456')").getValue(context,Boolean.class);        //调用私有方法,将发生错误        //boolean isCorrect2=parser.parseExpression("validatePassword2('123456')").getValue(context,Boolean.class);        //调用静态方法        boolean isCorrect3=parser.parseExpression("validatePassword3('123456')").getValue(context,Boolean.class);        //调用对象方法,可变参数列表        boolean isCorrect4=parser.parseExpression("addInterests('Js','C')").getValue(context, Boolean.class);        System.out.println(substring);        System.out.println(index);        System.out.println(isCorrect);        System.out.println(isCorrect3);        System.out.println(isCorrect4);    }}
操作符解析

1关系操作符

public class OperatorExprSample {    /**     * 关系操作符解析     * @param args     */    public static void main(String[] args){        ExpressionParser parser=new SpelExpressionParser();        //关系操作符        boolean trueValue= parser.parseExpression("2==2").getValue(Boolean.class);        boolean falseValue=parser.parseExpression("2<-5.0").getValue(Boolean.class);        System.out.println(trueValue);        System.out.println(falseValue);        //字符串关系比较        trueValue=parser.parseExpression("\"back\"<\"block\"").getValue(Boolean.class);        //instanceof运算符        falseValue=parser.parseExpression("'xyz' instanceof T(int)").getValue(Boolean.class);        System.out.println(trueValue);        System.out.println(falseValue);        //正则匹配运算,前面为字符串,后面为正则表达式        trueValue=parser.parseExpression("'5.00' matches '^?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);        falseValue=parser.parseExpression("'5.0067' matches '\\^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);        System.out.println(trueValue);        System.out.println(falseValue);    }}
2逻辑操作符

public class MethodExprSample1 {    public static void main(String[] args){        User user=new User();        ExpressionParser parser=new SpelExpressionParser();        EvaluationContext context=new StandardEvaluationContext(user);        //与操作,结果为false        boolean falseValue=parser.parseExpression("true && false").getValue(Boolean.class);        String expression="isVipMember('tom') and isVipMember('jony')";        boolean trueValue=parser.parseExpression(expression).getValue(context,Boolean.class);        //或操作,结果为true        trueValue=parser.parseExpression("true or false").getValue(Boolean.class);        //取非操作,结果为false        falseValue=parser.parseExpression("!true").getValue(Boolean.class);    }}
3算术运算操作符

public class OperatorExprSample1 {    public static void main(String[] args){        User user=new User();        ExpressionParser parser=new SpelExpressionParser();        //加法操作,运行结果等于1        int two=parser.parseExpression("1+1").getValue(Integer.class);        String testString=parser.parseExpression("\"test\"+' '+\"string\"").getValue(String.class);        System.out.println(two);        System.out.println(testString);        //减法操作,运行结果等于4        int four=parser.parseExpression("1 - -3").getValue(Integer.class);        System.out.println(four);        //减法操作,运行结果等于-9000        double d=parser.parseExpression("1000.00-1e4").getValue(Double.class);        System.out.println(d);        //乘法操作,运行结果等于6        int six=parser.parseExpression("-2*-3").getValue(Integer.class);        System.out.println(six);        //乘法操作,运行结果等于24.0        double twentyFour=parser.parseExpression("2.0*3e0*4").getValue(Double.class);        System.out.println(twentyFour);        //除法操作,运算结果等于-2        int minusTwo=parser.parseExpression("6/-3").getValue(Integer.class);        System.out.println(minusTwo);        //除法操作,运算结果等于1.0        double one=parser.parseExpression("8.0/4e0/2").getValue(Double.class);        System.out.println(one);        //求余操作,运算结果等于3        int three=parser.parseExpression("7%4").getValue(Integer.class);        one=parser.parseExpression("8/5%2").getValue(Integer.class);        System.out.println(three);        System.out.println(one);        //优先级算术运算,运行结果等于-21        int minusTwentyOne=parser.parseExpression("1+2-3*8").getValue(Integer.class);        System.out.println(minusTwentyOne);    }}
安全导航操作符:

public class SafeExprSample {    /**     * 为了避免繁琐的空对象验证,可采用安全导航操作符,它只会返回null而不是抛出异常。     * 其格式是在获取对象属性操作符"."前面添加一个"?"     * @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();        StandardEvaluationContext context=new StandardEvaluationContext(user);        String city= parser.parseExpression("PlaceOfBirth?.city").getValue(context,String.class);        System.out.println(city);        user.setPlaceOfBirth(null);        //不会抛出异常        city=parser.parseExpression("PlaceOfBirth?.city").getValue(context,String.class);        System.out.println(city);    }}
三元操作符:

public class IfThenElseExprSample {    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();        StandardEvaluationContext context=new StandardEvaluationContext(user);        String expression="userName == 'tom' ? credits+10:credits";        Integer credits=parser.parseExpression(expression).getValue(context,Integer.class);        System.out.println(credits);    }}
Elvis操作符:

public class ElvisExprSample {    /**     * 如果左边的变量取值为null,就取value值,否则就取var变量自身的值。     * @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();        StandardEvaluationContext context=new StandardEvaluationContext(user);        String userName=parser.parseExpression("userName?:'用户名为空'").getValue(context,String.class);        System.out.println(userName);        user.setUserName(null);        userName=parser.parseExpression("userName?:'用户名为空'").getValue(context,String.class);        System.out.println(userName);    }}
赋值:

public class ObjectExprSample {    public static void main(String[] args){        User user=new User();        user.setUserName("tom");        ExpressionParser parser=new SpelExpressionParser();        EvaluationContext context=new StandardEvaluationContext(user);        //通过setValue赋值        parser.parseExpression("userName").setValue(context,"jony");        System.out.println(user.getUserName());        //通过表达式赋值        parser.parseExpression("userName='anyli'").getValue(context);        System.out.println(user.getUserName());    }}
类型:

public class ObjectExprSample1 {    public static void main(String[] args){        ExpressionParser parser=new SpelExpressionParser();        //加载java.lang.Sring        Class stringClass=parser.parseExpression("T(java.lang.String)").getValue(Class.class);        System.out.println(stringClass==java.lang.String.class);        //加载com.smart.spel.User        Class userClass=parser.parseExpression("T(com.smart.spel.User)").getValue(Class.class);        System.out.println(userClass==com.smart.spel.User.class);        //T操作符还可以直接调用类静态方法        Object randomValue=parser.parseExpression("T(java.lang.Math).random()").getValue();        System.out.println(randomValue);    }}
构造器:用于创建一个对象

public class ObjectExprSample2 {    public static void main(String[] args){        ExpressionParser parser=new SpelExpressionParser();        User user=parser.parseExpression("new com.smart.spel.User()").getValue(User.class);        System.out.println(user.getUserName());    }}
变量:

public class ObjectExprSample3 {    public static void main(String[] args){        User user=new User();        ExpressionParser parser=new SpelExpressionParser();        EvaluationContext context=new StandardEvaluationContext(user);        //为newUserName变量设置新值        context.setVariable("newUserName","jony");        //取变量值,并赋值        parser.parseExpression("userName=#newUserName").getValue(context);        System.out.println(user.getUserName());        System.out.println(user.getNewUserName());        //this变量使用        List
credits=new ArrayList
(); credits.addAll(Arrays.asList(150,100,990,50,110,130,70)); context.setVariable("credits",credits); List
creditsGreater100= (List
)parser.parseExpression("#credits.?[#this>100]").getValue(context); System.out.println(creditsGreater100); }}
集合过滤:

/** * 集合过滤 * 过滤条件是针对集合内的每一个元素进行比较 */public class ObjectExprSample4 {    public static void main(String[] args){        ExpressionParser parser=new SpelExpressionParser();        EvaluationContext context=new StandardEvaluationContext();        Map
creditsMap=new HashMap
(); creditsMap.put("Tom",95); creditsMap.put("Jony",110); creditsMap.put("Morin",85); creditsMap.put("Mose",120); creditsMap.put("Morrow",60); context.setVariable("credits",creditsMap); Map
creditsGreater100= (Map
)parser.parseExpression("#credits.?[value>90]").getValue(context); System.out.println(creditsGreater100); }}

集合转换:

public class ObjectExprSample5 {    public static void main(String[] args){        ExpressionParser parser=new SpelExpressionParser();        EvaluationContext context=new StandardEvaluationContext();        List
credits=new ArrayList
(); credits.addAll(Arrays.asList(150,100,90,50,110,130,70)); context.setVariable("credits",credits); List
creditsGreater100= (List
)parser.parseExpression("#credits.![#this>100]").getValue(context); System.out.println(creditsGreater100); }}

转载地址:http://hojqi.baihongyu.com/

你可能感兴趣的文章
Wireless Network Hacks & Mods For Dummies
查看>>
Programming INDIGO
查看>>
System Analysis, Design, and Development: Concepts, Principles, and Practices
查看>>
How to Run Successful Projects in Web Time
查看>>
Sams Teach Yourself SQL Server(TM) 2005 Express in 24 Hours
查看>>
Beginning Apache Struts: From Novice to Professional
查看>>
Expert Spring MVC and Web Flow (Expert)
查看>>
A Developer's Guide to SQL Server 2005
查看>>
Active Directory Cookbook, 2nd Edition
查看>>
Designing and Developing Scalable IP Networks
查看>>
Bluetooth Demystified
查看>>
SWT (Developer's Notebook)
查看>>
Java 2: The Complete Reference, Fifth Edition
查看>>
Java Programming on Linux
查看>>
Microsoft Access VBA Programming for the Absolute Beginner, Second Edition
查看>>
Content Networking: Architecture, Protocols, and Practice
查看>>
Managing Agile Projects
查看>>
Creating a Digital Home Entertainment System with Windows Media Center
查看>>
Java Concurrency in Practice
查看>>
Red Hat Fedora 5 Unleashed
查看>>