【发布时间】:2014-07-16 16:12:18
【问题描述】:
我在一些使用 Groovy 类别的 DSL 下工作,我想找到一种方法将我的 DSL 与 groovy shell 一起使用,而无需为每个命令显式编写 use(MyCategory){ myObject.doSomething() }。
例如,假设我有以下玩具类别:
class MyCategory {
static Integer plus(Integer integer, String string){
return integer + Integer.valueOf(string)
}
}
那么,我可以通过以下方式在groovysh中使用这个类别:
groovy> use(MyCategory){ 2 + '3' } //gives 5
那么,有没有办法为所有groovysh 命令全局设置MyCategory,这样就不必每次都将我的命令包装在use(MyCategory) { ... } 中?例如:
groovy> useGlobally(MyCategory); //call something like this only once
groovy> 2 + '3' //automatically uses MyCategory and gives 5
【问题讨论】:
标签: groovy dsl groovyshell