Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting a Groovy Grails-service into a Java class

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.)

like image 509
calebds Avatar asked Jan 18 '23 05:01

calebds


2 Answers

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

like image 75
Tomas Lin Avatar answered Jan 22 '23 21:01

Tomas Lin


If you want to inject the Grails service into the Java class without using applicationContext.getBean("exampleService"), the Java class must itself be a Spring bean, and you should wire the two together in either resources.groovy or resources.xml.

If the above doesn't make a lot of sense to you, you might want to read up on the basics of Spring dependency injection.

like image 32
Dónal Avatar answered Jan 22 '23 20:01

Dónal