zoukankan      html  css  js  c++  java
  • Spring Bean Expression Language(EL)

    1, Add dependency.

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${spring.version}</version>
    </dependency>
    

      

    2. Test Cases

    import org.junit.Test;
    import org.springframework.expression.EvaluationContext;
    import org.springframework.expression.Expression;
    import org.springframework.expression.ExpressionParser;
    import org.springframework.expression.spel.SpelCompilerMode;
    import org.springframework.expression.spel.SpelParserConfiguration;
    import org.springframework.expression.spel.standard.SpelExpression;
    import org.springframework.expression.spel.standard.SpelExpressionParser;
    import org.springframework.expression.spel.support.StandardEvaluationContext;
    
    
    public class SpringELTest {
    
        @Test
        public void testEvaluationContext() {
    
        Account account = new Account("Deniro");
        ExpressionParser parser = new SpelExpressionParser();
        EvaluationContext context = new StandardEvaluationContext(account);
        Expression expression = parser.parseExpression("name");
        String result = expression.getValue(context, String.class);
        System.out.println("result:" + result);
    
        }
    
        @Test
        public void test1() {
        ExpressionParser parser = new SpelExpressionParser();
        Expression expression = parser.parseExpression("'SpEL'.concat(' thinking')");
        String result = (String) expression.getValue();
        System.out.println("result:" + result);
        }
    
        @Test
        public void test2() {
        ExpressionParser parser = new SpelExpressionParser();
        Expression expression = parser.parseExpression("6+2");
        Integer result = (Integer) expression.getValue();
        System.out.println("result:" + result);
        }
    
        @Test
        public void test3() {
    
        // Spel 解析配置器
        SpelParserConfiguration configuration = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, SpringELTest.class.getClassLoader());
    
        // 解析器
        SpelExpressionParser parser = new SpelExpressionParser(configuration);
    
        // 上下文
        EvaluationContext context = new StandardEvaluationContext(new Account("Deniro"));
    
        // 表达式
        String expression = "getName()";
    
        // 解析表达式
        SpelExpression spelExpression = parser.parseRaw(expression);
    
        System.out.println(spelExpression.getValue(context));
        }
    
    }
    public class Account {
    
    
        private String name;
    
        public Account(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    }
    

     

    import java.util.ArrayList;
    import java.util.List;

    import org.springframework.expression.EvaluationException;
    import org.springframework.expression.ExpressionParser;
    import org.springframework.expression.ParseException;
    import org.springframework.expression.spel.standard.SpelExpressionParser;
    import org.springframework.expression.spel.support.StandardEvaluationContext;

    public class GenericConvertExample {
    public List<Integer> nums = new ArrayList<Integer>();

    public static void main(String[] args) {

    GenericConvertExample example = new GenericConvertExample();
    example.nums.add(1);

    //创建表达式上下文
    StandardEvaluationContext context = new StandardEvaluationContext(example);
    //创建表达式解析器
    ExpressionParser parser = new SpelExpressionParser();

    String expression = "nums[0]";
    //自动将 2 转换为 Integer 类型
    parser.parseExpression(expression).setValue(context, 2);
    System.out.println("nums:" + example.nums);

    //抛出 ConverterNotFoundException
    try {
    parser.parseExpression(expression).setValue(context, true);
    } catch (EvaluationException e) {
    e.printStackTrace();
    } catch (ParseException e) {
    e.printStackTrace();
    }

    }
    }

     

  • 相关阅读:
    CodeForces1152CNeko does Maths
    π的计算公式
    IFS变量对加双引号和不加双引号变量的区别对待
    各种距离
    ADO.NET Data Service 二 绑定数据类
    向客户端注入JavaScript代码
    Ajax ModelPopu and Progress 示例学习
    Sliverlight 入门教程七
    (牛人莫入)Jquery plugin 多文件上传
    自定义控件的验证使用
  • 原文地址:https://www.cnblogs.com/hcoding/p/spel.html
Copyright © 2011-2022 走看看