在Grails 2.5.4中将AbstractPersistenceEventListener子类放在哪里? [英] Where to put AbstractPersistenceEventListener subclasses in Grails 2.5.4?

查看:128
本文介绍了在Grails 2.5.4中将AbstractPersistenceEventListener子类放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 AbstractPersistenceEventListener 子类化,所以我可以在Grails 2.5.4 中注册自定义事件侦听器。但是,我应该将这些子类放在哪里?

I want to subclass AbstractPersistenceEventListener so I can register custom event listeners in Grails 2.5.4. However, where should I place these subclasses?

最重要的是,我希望这些事件侦听器使用自动装配的bean,尤其是服务bean。如果我将这些类放在 src / groovy 中,似乎我必须在 resources.groovy 中手动注册Bean,这是我想做的一个额外步骤避免。

Most importantly I want these event listeners to use autowired beans, especially service beans. If I put these classes in src/groovy, it seems I'll have to manually register the beans in resources.groovy, which is an extra step I'd like to avoid.

推荐答案

帖子显示了如何在不使用插件的情况下在grails中注册自定义事件侦听器,该插件可以在 doWithApplicationContext 中进行注册。 code>闭包而不是在 Bootstrap.groovy 内完成。

This post shows how you can register a custom event listener in grails if you are not doing it using a plugin where you can register it inside doWithApplicationContext closure instead of doing it inside Bootstrap.groovy.

您应该将这些类放在src / groovy。不,您不需要在 resources.groovy 内再次将侦听器注册为bean或任何其他bean。但是,如果您的侦听器需要使用任何bean,则可以为其声明一个字段并在注册侦听器时对其进行初始化。例如,如果您需要在监听器中注入 grailsApplication bean,然后在注册监听器时执行以下操作:

And you should put these classes under src/groovy. And no, you don't need to register the listener as a bean or any other beans again inside resources.groovy. However if your listener need to use any bean then you can declare a field for that and initialize it when you are registering the listener. For eg if you need to inject grailsApplication bean in your listener then do this while registering your listener:

def grailsApplication

def init = { servletContext ->
    def applicationContext = servletContext.getAttribute(ApplicationAttributes.APPLICATION_CONTEXT)
    applicationContext.eventTriggeringInterceptor.datastores.each { k, datastore ->
    GormEventListener listener = new GormEventListener(datastore)
    listener.with{
        application = grailsApplication
    }
    applicationContext.addApplicationListener(listener)
}

这篇关于在Grails 2.5.4中将AbstractPersistenceEventListener子类放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆