Spel表达式学习

84 阅读1分钟
public class AppTest{
    public static void main(String[] args) throws NoSuchMethodException {
        specialOperator();
    }


    /**
     * 字面量表达式
     */
    private static void literalExpression(){
        ExpressionParser parser = new SpelExpressionParser();
        Boolean var = parser.parseExpression("true").getValue(Boolean.class);
        System.out.println(var);
    }
    /**
     * 不可变通过StandardEvaluationContext储存内容
     */
    private static void fromImmutableObject() {
        ExpressionParser parser = new SpelExpressionParser();
        Expression expression = parser.parseExpression("name.toUpperCase()");

        Student stu = Student.builder().name("jonny").age(18).build();
        // 构建StandardEvaluationContext比较昂贵,建议缓存
        StandardEvaluationContext context = new StandardEvaluationContext(stu);
        System.out.println(expression.getValue(context));
    }

    /**
     * 可变直接从对象读取
     */
    private static void fromMutableObject() {
        ExpressionParser parser = new SpelExpressionParser();
        Expression expression = parser.parseExpression("name.toUpperCase()");

        Student stu = Student.builder().name("jonny").age(18).build();
        System.out.println(expression.getValue(stu));
    }

    /**
     * 自带的ConversionService支持类型转换
     */
    private static void autoTypeConversion(){
        class Sample{
            public final List<Integer> data = new ArrayList<>();
        }
        Sample sample = new Sample();
        sample.data.add(1);
        ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext(sample);
        parser.parseExpression("data[0]").setValue(context,"2");
        System.out.println(sample.data);
    }

    /**
     * 行列list
     */
    private static void inlineList(){
        ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext();
        Expression expression = parser.parseExpression("{'a','b','c'}");
        System.out.println(expression.getValue(context));
    }

    /**
     * 调用方法
     */
    private static void methods(){
        class Functions{
            public String hello(){
                return "hello";
            }
            public String hello(String name){
                return "hello "+name;
            }
        }
        Functions functions = new Functions();
        ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext(functions);
        System.out.println(parser.parseExpression("hello()").getValue(context, String.class));
        System.out.println(parser.parseExpression("hello('jonny')").getValue(context, String.class));
    }

    /**
     * 逻辑运算符
     */
    private static void logicOperation(){
        ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext();
        Expression expression = parser.parseExpression("1>2 or 1 < 2");
        System.out.println(expression.getValue(Boolean.class));
    }

    /**
     * 直接执行静态方法
     */
    private static void types(){
        ExpressionParser parser = new SpelExpressionParser();
        System.out.println(parser.parseExpression("T(java.util.UUID).randomUUID()").getValue());
    }

    /**
     * 通过#读取变量。
     * #root 表示根对象,#this表示当前对象
     * .? 表示遍历集合判断是否符合条件
     */
    private static void variables(){
        List<Integer> numberList = Lists.list(1,2,4,5,6);
        ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setVariable("numberList", numberList);
        System.out.println(parser.parseExpression("#numberList.?[#this % 2 == 0]").getValue(context));
    }

    /**
     *
     * 调用静态方法。需要通过registerFunction进行注册,需要为static方法
     */
    private static void functions() throws NoSuchMethodException {
        class Functions{
            public String hello(){
                return "hello";
            }
            public static String hello(String name){
                return "hello "+name;
            }
        }
        Method hello = Functions.class.getDeclaredMethod("hello", String.class);
        ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext();
        context.registerFunction("hello",hello);
        System.out.println(parser.parseExpression("#hello('jonny')").getValue(context,String.class));

    }

    /**
     * “?:”操作符 当前面的变量为null的时候取后面的值
     * “?.”操作符 当前面的变量为null的时候返回null
     */
    private static void specialOperator(){
        class Sample{
            public String name;
        }
        Sample sample = new Sample();
        ExpressionParser parser = new SpelExpressionParser();
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("sample",sample);
        System.out.println(parser.parseExpression("#sample.name?:'nnull'").getValue(context,String.class));
        System.out.println(parser.parseExpression("#sample?.name").getValue(context, String.class));
    }
}