如何创建MVEL评估上下文?

时间:2012-08-06 19:23:56

标签: mvel

我想按顺序解析MVEL:

HashMap myData = new HashMap(){{ put("x", 1); }}

eval("y = 2", ...) // assign value to y
...
eval("x + y", myData, ...) // expect 3

这可能吗?如果是这样,怎么样?

1 个答案:

答案 0 :(得分:2)

只需在表达式评估中共享变量解析器:

VariableResolverFactory vars = new MapVariableResolverFactory(new HashMap(){{put(“x”,1);}});

MVEL.eval(“y = 2”,vars): MVEL.eval(“x + y”,vars);