文档中的自定义事件侦听器示例

文档中的自定义事件侦听器示例

本文介绍了Grails 文档中的自定义事件侦听器示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Bootstrap.groovy 中添加自定义 GORM 事件侦听器类,如 Grails 文档 但它对我不起作用.这是直接来自文档的代码:

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)
    }
}

当我运行它时,编译器抱怨 application 和 applicationContext 为空.我曾尝试将它们添加为类级别成员,但它们并没有像服务风格那样神奇地连接起来.我到目前为止最接近的是:

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)
    }
}

但我仍然收到错误:java.lang.NullPointerException:无法在空对象上获取属性数据存储".

感谢阅读...

2.2.1 版

推荐答案

如果你这样做:

ctx.getBeansOfType(Datastore).values().each { Datastore d ->
   ctx.addApplicationListener new MyPersistenceListener(d)
}

这应该不需要安装 Hibernate 插件即可工作

This should work without needing the Hibernate plugin installed

这篇关于Grails 文档中的自定义事件侦听器示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 14:19