Access grailsApplication or Service in groovy class

后端 未结 4 797
长情又很酷
长情又很酷 2021-02-14 15:20

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

4条回答
  •  春和景丽
    2021-02-14 15:43

    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
    

提交回复
热议问题