Java Script Engine
1 | public static void main(String[] agrs) throws ScriptException { |
输出结果:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24-------Engine bindings scope--------------
Args (Global bindings scope) > a=5
Args (Engine bindings scope) > z=1
Args (Engine bindings scope) > y=20.4
Args (Engine bindings scope) > x=20
script > x*y+z = 409.0
script > x*(y+z) = 428.0
script > a+x*(y+z) = 433.0
-------Local bindings scope--------------
Args (Global bindings scope) > a=5
Args (Engine bindings scope) > println=sun.org.mozilla.javascript.internal.InterpretedFunction@45a23f67
Args (Engine bindings scope) > context=javax.script.SimpleScriptContext@1ef0a6e8
Args (Engine bindings scope) > z=1
Args (Engine bindings scope) > print=sun.org.mozilla.javascript.internal.InterpretedFunction@495dd936
Args (Engine bindings scope) > y=20.4
Args (Engine bindings scope) > x=20
Args (Local bindings scope) > z=1
Args (Local bindings scope) > y=20.4
Args (Local bindings scope) > x=20
script > x*y+z = 7.0
script > x*(y+z) = 8.0
script > a+x*(y+z) = 13.0
Process finished with exit code 0
Bindings变量的有效范围
- Global对应到ScriptEngineFactory,通过
scriptEngine.getBindings(ScriptContext.GLOBAL_SCOPE)
获得。 - Engine对应到ScriptEngine,通过
scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE)
获得。 - Local Binding每一次执行script,通过
scriptEngine.createBindings()
获得。
使用场景适用于
- 规则引擎
- 流程流转条件判定
观点仅代表自己,期待你的留言。