平时工作中经常要用到表达式值的计算问题,Spring框架中提供了Spring Expression Language(简称 SpEL)机制,可以很方便快捷的实现表达式值的计算;
SpEL机制需要引入Spring-expression包。下面列举其应用的两个小Demo;
1、数值计算:
String expressionStr = "19 + 26"; ExpressionParser parpser = new SpelExpressionParser(); Expression exp = parpser.parseExpression(expressionStr); System.out.println(exp.getValue(String.class));
2、表达式值计算:
1 ExpressionParser parpser = new SpelExpressionParser(); 2 EvaluationContext context = new StandardEvaluationContext(); 3 context.setVariable("user", "bill gates"); 4 5 Expression exp = parpser.parseExpression("Hello, {{#user}}", new TemplateParserContext("{{", "}}")); 6 System.out.println(exp.getValue(context, String.class));
3、参考资料:
https://blog.csdn.net/f641385712/article/details/90812967
标签:parpser,String,--,Spring,exp,new,表达式 From: https://www.cnblogs.com/laoxia/p/17699786.html