当前位置 : 主页 > 编程语言 > c++ >

spring expression languate

来源:互联网 收集:自由互联 发布时间:2021-07-03
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.
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.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();
		Map
 
   params = 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; } }
  
 
网友评论