问题描述
我在Groovy中实现了一个Grails服务,我想在Web应用程序中注入Java类。我知道我可以通过applicationContext.getBean(exampleService)获取Java中的bean,但是在编译时,ExampleService是未知的。我可以简单地导入服务?它似乎没有声明一个典型的包。
(我对Grails和Java Web来说相当新鲜,所以任何事情都有助于我了解后面的内容
推荐的方法是将Grails服务提取到一个接口中,然后注入这个服务通过Spring进入你的java类。请参阅用户指南 -
I have a Grails-service implemented in Groovy, which I'd like to inject into a Java class, in the web application. I know I can get the bean in Java via applicationContext.getBean("exampleService"), but the type ExampleService is unknown at compile time.
Can I simply import the service? It doesn't seem to declare a typical package.
(I'm fairly new to Grails and the Java Web so anything to help my understanding of what's going on behind the scenes here is greatly appreciated.)
The recommended approach is to extract the Grails service into an interface, and then inject this service into your java class via Spring. See the user guide - http://www.grails.org/doc/1.3.x/guide/8.%20The%20Service%20Layer.html#8.4%20Using%20Services%20from%20Java
这篇关于将Groovy Grails服务注入Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!