doWithDynamicMethods访问服务

时间:2013-04-14 01:58:01

标签: grails grails-plugin

我无法在doWithDynamicMethod中访问特定服务。

def doWithDynamicMethods = { ctx ->
        def nodeDriverProxy  = application.getServiceClass('NodeDriverProxyService')
        application.domainClasses.each{ cClass ->
            delegate.log.warn('cClass: ' + cClass.toString())
            cClass.metaClass.cacheInstance = { domainIns->
                delegate.log.warn "domain is being cached id: ? with version ?"
                nodeDriverProxy.registerUpdate(domainIns.id, domainIns.version)
            }

变量nodeDriveProxy为null或者我在访问'application.serviceClasses'时遇到异常我做错了什么?谢谢

1 个答案:

答案 0 :(得分:2)

您不希望GrailsServiceClass,您想要服务实例。您有ctx变量,即Spring ApplicationContext,因此它将是

def doWithDynamicMethods = { ctx ->
   def service = ctx.nodeDriverProxyService
   application.domainClasses.each { cClass ->
      delegate.log.warn('cClass: ' + cClass.toString())
      cClass.metaClass.cacheInstance = { domainIns ->
         delegate.log.warn "domain is being cached id: ? with version ?"
         service.registerUpdate(domainIns.id, domainIns.version)
      }
   }
}