package com.feng.spring.chapter2.helloworld;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
public class HelloTest {
@Test
public void helloWorld(){
ExpressionParser parser = new SpelExpressionParser();//创建解析器
Expression expression = parser.parseExpression("('Hello '+'World').concat(#end)");//解析表达式
EvaluationContext context = new StandardEvaluationContext();//构造上下文
context.setVariable("end", "!");
Assert.assertEquals("Hello World!", expression.getValue(context)); //求值,根据上下文获得表达式
}
}SPEL的helloWorld
最新推荐文章于 2024-12-11 09:37:50 发布
本文介绍如何在Spring框架中使用SpEL表达式进行动态代码生成,通过解析器解析表达式并根据上下文求值得到最终结果。
1892

被折叠的 条评论
为什么被折叠?



