本文介绍了在groovy类中访问grailsApplication或Service的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 依赖注入不适用于 src下的常规类/常规。您可以像这样使用 ApplicationHolder 访问 grailsApplication 。

  import org.codehaus.groovy.grails.commons.ApplicationHolder 
$ b $ def grailsApplication = ApplicationHolder.application

您可以像这样访问所有服务:

  def allServicesArtefacts = grailsApplication.services 


I am trying to access grailsApplication in groovy class under src/groovy but I get a null pointer exception. I also tried to inject a service into the same class and same result. How can I access grailsApplication or a service from groovy class? (I am using Grails 1.3.7)

解决方案

Dependency injection does not work for groovy classes under src/groovy. You can get the access to grailsApplication using ApplicationHolder like this:

import org.codehaus.groovy.grails.commons.ApplicationHolder

def grailsApplication = ApplicationHolder.application

You can access all services like this:

def allServicesArtefacts = grailsApplication.services

这篇关于在groovy类中访问grailsApplication或Service的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 12:01