TestSpringExpr import java.util.HashMap;import java.util.Map;import org.springframework.expression.ExpressionParser;import org.springframework.expression.spel.standard.SpelExpressionParser;import org.springframework.expression.spel.support.
import java.util.HashMap; import java.util.Map; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; public class TestSpringExpr { public static void main(String[] args) throws Exception { testVariable(); testFunction(); } private static void testVariable() { ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); Mapparams = new HashMap<>(); params.put("aa", 11.0); params.put("bb", 22); params.put("cc", 22); context.setVariable("params", params); String expr = "0.1 * #params.get('aa')/#params.get('bb')"; Object obj = parser.parseExpression(expr).getValue(context); System.out.println(obj); } private static void testFunction() throws Exception { ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); Map params = new HashMap<>(); params.put("aa", 11.0); params.put("bb", 22); params.put("cc", 22); context.setVariable("params", params); context.registerFunction("divide", TestSpringExpr.class.getDeclaredMethod("divide", new Class[] { Double.class, Double.class })); String expr = "#divide(#params.get('aa'), #params.get('bb'))"; Object obj = parser.parseExpression(expr).getValue(context); System.out.println(obj); } private static Double divide(Double a, Double b) { return a / b; } }