GroovyShell 脚本应用
GroovyShell 脚本应用
在Groovy中你可以使用GroovyShell对Groovy脚本和表达式进行调试,GroovyShell允许你通过Binding对象传入或传出变量。下面通过三种方式,传递参数。
def groovyShellTest1()
{
def city = new City()
def shell = new GroovyShell()
def closure
def result
def shellContext = "{city->city.name='haerbin';city.tel='123456';city.save();return city;}"
try
{
closure = shell.evaluate(shellContext)
result = closure(city)
println "result = " + result
return result
}
catch (Exception e)
{
println e
}
}
def groovyShellTest2()
{
def city = new City()
Binding binding = new Binding();
binding.setVariable("city", city);
def shell = new GroovyShell(binding)
def result
def shellContext = "city.name='haerbin2';city.tel='876543';city.save();return city;"
try
{
result = shell.evaluate(shellContext)
println "result = " + result
return result
}
catch (Exception e)
{
println e
}
}
def groovyShellTest3()
{
Binding binding = new Binding();
binding.setVariable("age", 10);
def shell = new GroovyShell(binding)
def result
try
{
result = shell.evaluate("println 'Hello Groovy !';return age * 10")
println "result = " + result
return result
}
catch (Exception e)
{
println e
}
} 相关推荐
laisean 2020-11-11
zhangjie 2020-11-11
大牛牛 2020-10-30
firefaith 2020-10-30
liguojia 2020-10-20
wangzhaotongalex 2020-10-20
CARBON 2020-10-20
JohnYork 2020-10-16
xiaonamylove 2020-10-16
Julyth 2020-10-16