55import org .springframework .core .DefaultParameterNameDiscoverer ;
66import org .springframework .core .ParameterNameDiscoverer ;
77import org .springframework .expression .ExpressionParser ;
8+ import org .springframework .expression .common .TemplateParserContext ;
89import org .springframework .expression .spel .standard .SpelExpressionParser ;
910import org .springframework .expression .spel .support .StandardEvaluationContext ;
1011
2829 *
2930 * <p>
3031 * <b>Example expressions:</b>
31- *
32+ *
3233 * <pre>
3334 * \"'user:' + #userId\" // Concatenates literal with argument
3435 * \"'result:' + #result.id\" // Uses property of returned object
4748 * @see org.springframework.core.DefaultParameterNameDiscoverer
4849 */
4950public class SpelExpressionEvaluator {
50- private static final ParameterNameDiscoverer paramNameDiscoverer = new DefaultParameterNameDiscoverer ();
51- private static final ExpressionParser parser = new SpelExpressionParser ();
51+ private static final ParameterNameDiscoverer PARAMETER_NAME_DISCOVERER = new DefaultParameterNameDiscoverer ();
52+ private static final ExpressionParser PARSER = new SpelExpressionParser ();
53+ private static final TemplateParserContext TEMPLATE_PARSER_CONTEXT = new TemplateParserContext ();
5254
5355 /**
5456 * Evaluates a SpEL expression within the context of a join point and method
@@ -92,7 +94,7 @@ public String evaluate(String template, JoinPoint jp, Object result) {
9294 * @return the string result of the expression evaluation
9395 */
9496 public String evaluate (String template , Method method , Object result , Object ... args ) {
95- String [] paramNames = paramNameDiscoverer .getParameterNames (method );
97+ String [] paramNames = PARAMETER_NAME_DISCOVERER .getParameterNames (method );
9698
9799 StandardEvaluationContext context = new StandardEvaluationContext ();
98100 context .setVariable ("args" , args );
@@ -105,6 +107,11 @@ public String evaluate(String template, Method method, Object result, Object...
105107 }
106108 }
107109
108- return parser .parseExpression (template ).getValue (context , String .class );
110+ boolean isTemplate = template .contains ("#{" );
111+
112+ if (isTemplate ) {
113+ return PARSER .parseExpression (template , TEMPLATE_PARSER_CONTEXT ).getValue (context , String .class );
114+ }
115+ return PARSER .parseExpression (template ).getValue (context , String .class );
109116 }
110117}
0 commit comments