无法在src/groovy中注入grails应用程序上下文

无法在src/groovy中注入grails应用程序上下文,grails,grails-plugin,Grails,Grails Plugin,我已经创建了自定义事件监听器,该监听器扩展了Grails文档中描述的AbstractPersistenceEventListener,但是工作正常。如果执行以下操作,我将无法在此侦听器中注入服务 def context = ServletContextHolder.servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT) def myService = context.myService

我已经创建了自定义事件监听器,该监听器扩展了Grails文档中描述的AbstractPersistenceEventListener,但是工作正常。如果执行以下操作,我将无法在此侦听器中注入服务

    def context = ServletContextHolder.servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT)
def myService = context.myService 
“上下文”返回null

有什么问题吗。非常感谢。
p、 s.GRails 2.1.2

注册时,将applicationContext作为构造函数参数传递给侦听器

上下文返回null或上下文为null?对不起,我的英语不好。上下文为null或ServletContextHolder.servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_context)返回nullThank you Todd。我有点困惑…在这么多的魔法中:)我没有参数,而是保持构造函数的原样,并在注册event.def doWithApplicationContext={applicationContext->application.mainContext.eventTriggeringInterceptor.datastores.each之前在初始化后分配服务{k,datastore->def cacheListener=new cacheListener(datastore)cacheListener.nodeDriverProxyService=applicationContext.nodeDriverProxyService applicationContext.addApplicationListener cacheListener