1

我是 Grails 新手,我正在使用 Grails 2.0.1。我想为域类的对象更改添加持久性事件侦听器,因此我尝试了用户指南中给出的 Bootstrap.groovy 中的代码:

def init = {
    applicationContext.addApplicationListener(new FooBarListener())
}

我收到以下错误消息:

错误 context.GrailsContextLoader - 执行引导程序时出错:没有这样的属性:类的 applicationContext:BootStrap

如何从 BootStrap 类中获取applicacionContext属性?或者文档是否过时并且有一种新的/更好的方法来添加域更改侦听器?

提前致谢。

4

3 回答 3

8

我知道的最短的方法是

class BootStrap {

   def grailsApplication

   def init = { servletContext ->
      def applicationContext = grailsApplication.mainContext
   }
}
于 2012-03-21T22:28:32.787 回答
2
import org.codehaus.groovy.grails.commons.ApplicationAttributes

class BootStrap {

    def init = {servletContext ->

        def applicationContext = servletContext.getAttribute(ApplicationAttributes.APPLICATION_CONTEXT) 
    }
}
于 2012-03-21T17:01:57.810 回答
-2

applicacionContext 必须在 BootStrap 中定义。以下应该工作

     class BootStrap {
       def applicacionContext 

    def init = {
     applicationContext.addApplicationListener(new FooBarListener())
    }

   }
于 2012-03-21T16:48:58.833 回答