Tuesday, March 18, 2014

GRAILS Injecting services or classes into groovy source

Assuming you have below service class in your grails service(grails-app/services) directory.

class MyService {    
   def printSomething() {
     println "Hello World!!!"

And we also have a groovy class under the src(src/groovy) directory and we wanted to inject MyService in this class like below.
class HelloWorld {
  MyService myService

  def printHelloWorld() {

Finally, we put our injection logic in resources.groovy(grails-app/conf/spring/resources.groovy)
import package.HelloWorld

beans = {
helloWorld(HelloWorld) {
        myService = ref("myService")