Grails 2.x服务注入Groovy / src [英] Grails 2.x service injection in Groovy/src

查看:109
本文介绍了Grails 2.x服务注入Groovy / src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Groovy / src类中注入我的服务。 normaln依赖注入无效:

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

...
def myService
...

我可以使用这个工具:

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

但ApplicationHolder已弃用。有没有更好的解决方案?

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

感谢您的任何建议

Thanks for any suggestion

推荐答案

检查以下Grails常见问题以从src / groovy中的源访问应用程序上下文 - http://grails.org/FAQ#Q:如何从src / groovy中的源访问应用程序上下文?

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?

没有ApplicationContextHolder类与ApplicationHolder等效。要从src / groovy中的Groovy类访问名为EmailService的服务类,请使用以下命令访问Spring bean:

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

这篇关于Grails 2.x服务注入Groovy / src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆