Grails 2.x service injection in Groovy/src

后端 未结 4 1136
醉酒成梦
醉酒成梦 2020-12-16 17:10

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

4条回答
  •  太阳男子
    2020-12-16 17:47

    Check following Grails FAQ to get access to the application context from sources in src/groovy - http://grails.org/FAQ#Q: How do I get access to the application context from sources in src/groovy?

    There is no ApplicationContextHolder class equivalent to ApplicationHolder. To access to a service class called EmailService from a Groovy class in src/groovy, access the Spring bean using:

    import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH
    import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes as GA
    def ctx = SCH.servletContext.getAttribute(GA.APPLICATION_CONTEXT)
    def emailService = ctx.emailService
    

提交回复
热议问题