Spring Bean Expression Language(EL)

Posted hcoding

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();


 

以上是关于Spring Bean Expression Language(EL)的主要内容,如果未能解决你的问题,请参考以下文章

Spring注释创建一个jexl2.Expression

SpringIOC容器装配Bean

Spring入门第一课:Spring基础与配置Bean

Spring使用ioc注解方式配置bean

Spring框架bean的注解管理方法

spring构造注入