Grails文档中的自定义事件侦听器示例 [英] Custom event listener example in Grails documentation
问题描述
我试图在 Bootstrap.groovy
中添加一个自定义的GORM事件监听器类,如 Grails文档,但它不适合我。这里是直接从文档代码:
def init = {
application.mainContext.eventTriggeringInterceptor.datastores.each {k,数据存储 - >
applicationContext.addApplicationListener new MyPersistenceListener(datastore)
}
}
当我运行它时,编译器会抱怨应用程序和applicationContext为空。我已经尝试将它们添加为类级别的成员,但它们并没有神奇地将服务风格连接起来。到目前为止,我得到的最接近的是:
def grailsApplication
def init = {servletContext - >
def applicationContext = servletContext.getAttribute(ApplicationAttributes.APPLICATION_CONTEXT)
grailsApplication.mainContext.eventTriggeringInterceptor.datastores.each {k,datastore - >
applicationContext.addApplicationListener new GormEventListener(datastore)
}
}
但我仍然收到错误: java.lang.NullPointerException:无法获得null对象的属性'datastores'。
感谢您阅读...
编辑:版本2.2.1
如果您这样做:
ctx.getBeansOfType(Datastore).values()。each {Datastore d - >
ctx.addApplicationListener new MyPersistenceListener(d)
}
需要安装Hibernate插件
I'm trying to add a custom GORM event listener class in Bootstrap.groovy
, as described in the Grails documentation but its not working for me. Here is the code straight from the docs:
def init = {
application.mainContext.eventTriggeringInterceptor.datastores.each { k, datastore ->
applicationContext.addApplicationListener new MyPersistenceListener(datastore)
}
}
When I run it, the compiler complains that application and applicationContext are null. I've tried adding them as class level members but they don't get magically wired up service-style. The closest I've got so far is:
def grailsApplication
def init = { servletContext ->
def applicationContext = servletContext.getAttribute(ApplicationAttributes.APPLICATION_CONTEXT)
grailsApplication.mainContext.eventTriggeringInterceptor.datastores.each { k, datastore ->
applicationContext.addApplicationListener new GormEventListener(datastore)
}
}
But I still get errors: java.lang.NullPointerException: Cannot get property 'datastores' on null object
.
Thanks for reading...
EDIT: version 2.2.1
If you do:
ctx.getBeansOfType(Datastore).values().each { Datastore d ->
ctx.addApplicationListener new MyPersistenceListener(d)
}
This should work without needing the Hibernate plugin installed
这篇关于Grails文档中的自定义事件侦听器示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!