码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Bean Expression Language(EL)

时间:2019-09-03 16:58:05      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:nts   think   getc   parser   junit   compiler   lis   org   private   

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();
}

}
}

 

Spring Bean Expression Language(EL)

标签:nts   think   getc   parser   junit   compiler   lis   org   private   

原文地址:https://www.cnblogs.com/hcoding/p/spel.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!