添加依赖
<dependency><groupId>com.googlecode.aviator</groupId><artifactId>aviator</artifactId><version>5.2.0</version>
</dependency>
编写代码
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;
import org.apache.commons.lang3.time.StopWatch;
import org.junit.Assert;
import org.junit.Test;import java.util.HashMap;
import java.util.Map;public class AviatorTest {@Testpublic void test() {Map<String, Object> env = new HashMap<String, Object>();env.put("A", 1);env.put("B", 2);env.put("C", 3);StopWatch stopWatch = new StopWatch();stopWatch.start();for (int i = 0; i < 10000; i++) {Object result = AviatorEvaluator.execute("A+B*C", env);Assert.assertEquals(result.toString(), "7");}stopWatch.stop();System.out.println(stopWatch.getTime());}@Testpublic void testWithCache() {Map<String, Object> env = new HashMap<String, Object>();env.put("A", 1);env.put("B", 2);env.put("C", 3);Expression expression = AviatorEvaluator.compile("A+B*C");StopWatch stopWatch = new StopWatch();stopWatch.start();for (int i = 0; i < 100000000; i++) {Object result = expression.execute(env);Assert.assertEquals(result.toString(), "7");}stopWatch.stop();System.out.println(stopWatch.getTime());}
}