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