【问题标题】:Where to put AbstractPersistenceEventListener subclasses in Grails 2.5.4?在 Grails 2.5.4 中将 AbstractPersistenceEventListener 子类放在哪里?
【发布时间】:2016-06-26 21:09:18
【问题描述】:

我想继承AbstractPersistenceEventListener,这样我就可以register custom event listeners in Grails 2.5.4。但是,我应该将这些子类放在哪里?

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

【问题讨论】:

    标签: grails conventions


    【解决方案1】:

    post 展示了如何在 grails 中注册自定义事件侦听器,如果您不使用插件在其中注册它,您可以在 doWithApplicationContext 闭包中注册,而不是在 Bootstrap.groovy 中注册。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多