jar包依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jexl</artifactId>
<version>2.1.1</version>
</dependency>
package com.yousong.common;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.jexl2.Expression;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.JexlEngine;
import org.apache.commons.jexl2.MapContext;
/**
* 动态加载方法
* @author ld
*
*/
public class DyMethodUtil {
public static Object invokeMethod(String jexlExp,Map<String,Object> map){
JexlEngine jexl=new JexlEngine();
Expression e = jexl.createExpression(jexlExp);
JexlContext jc = new MapContext();
for(String key:map.keySet()){
jc.set(key, map.get(key));
}
if(null==e.evaluate(jc)){
return "";
}
return e.evaluate(jc);
}
public static void main(String[] args) {
Map<String,Object> map=new HashMap<String,Object>();
map.put("money",2100);
String expression="money>=4000";
Object code = invokeMethod(expression,map);
System.err.println(code);
}
}
上面的代码很详细了,可以直接执行的demo。拿走不谢