Grails 2.x service injection in Groovy/src

kuceram picture kuceram · May 17, 2012 · Viewed 13k times · Source

I'd like to inject my service in Groovy/src class. The normaln dependency injection doesn't work:

...
def myService
...

I'm able to use this (it works):

def appCtx = ApplicationHolder.application.getMainContext()
def myService = appCtx.getBean("myService");

but the ApplicationHolder is deprecated. Is there any better solution?

Thanks for any suggestion

Answer

César picture César · Jan 9, 2014

The replacement of ApplicationHolder can be Holders, you can also use it in static scope:

import grails.util.Holders
...

def myService = Holders.grailsApplication.mainContext.getBean 'myService'