GroovyInterceptable (AOP) 和闭包
我有一个 grails 应用程序,其中的 Service 类继承自 Groovy 的 GroovyInterceptable:
class customerSerrvice implements GroovyInterceptable {
private List<Customer> customers
def invokeMethod(String name, args) {
log.debug "=======>INVOKING method [$name] with args:$args"
}
void foo() {
customers.each { doSomething(it) }
}
void doSomething(Customer cust) { log.debug "doSomething invoked with $cust" }
}
上面是一个大大简化的表示,但它为您提供了想法。如果我从另一个类直接调用 foo() 或 doSomething() ,则 invokeMethod 会像预期的那样被调用。然而,当 foo() 调用 doSomething() 时,该调用不会在 invokeMethod 中被拦截。
更改
如果我从customers.each { doSomething(it) }
为for(Customer cust:customers) { doSomething(cust) }
然后调用 invokeMethod 就可以了。
那么闭包和 GroovyInterceptable 有什么不兼容的地方吗?有什么方法可以让 invokeMethod 与闭包一起使用而不需要将它们全部更改掉吗?
谢谢
I've got a grails app with Service classes that inherit from Groovy's GroovyInterceptable:
class customerSerrvice implements GroovyInterceptable {
private List<Customer> customers
def invokeMethod(String name, args) {
log.debug "=======>INVOKING method [$name] with args:$args"
}
void foo() {
customers.each { doSomething(it) }
}
void doSomething(Customer cust) { log.debug "doSomething invoked with $cust" }
}
The above is a greatly simplified representation, but it gives you the idea. If I call foo() or doSomething() directly from another class, the invokeMethod gets called like it is supposed to. However, when foo() calls doSomething(), that call is not intercepted in invokeMethod.
If I change from
customers.each { doSomething(it) }
tofor(Customer cust: customers) { doSomething(cust) }
then the invokeMethod gets called just fine.
So is there something about closures and GroovyInterceptable that don't go together? Is there any way to get the invokeMethod to work with closures short of changing them all out?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确认为错误,旧链接:
http://jira.codehaus.org/browse/GROOVY-4610,新链接:https://issues.apache.org/jira/browse/GROOVY-4610
Confirmed as a bug, old link:
http://jira.codehaus.org/browse/GROOVY-4610, new link:https://issues.apache.org/jira/browse/GROOVY-4610