0

In Grails, we can inject a service into a controller. But often we might want to inject services elsewhere as well (for example in a quartz job).

I have faced issues injecting Grails services in a few places. One of them I can remember is in Quartz job where I was getting a null for service instance. I spent a whole day researching about it and ended up instantiating a service instance, rather than injecting it.

So whats the harm in instantiating a service instance instead of injecting it?

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
rahulserver
  • 10,411
  • 24
  • 90
  • 164
  • Ever heard of "Hollywood Principle"? Here is an [answer for reference](http://stackoverflow.com/a/17193754/2051952). – dmahapatro Jun 03 '15 at 20:10
  • @dmahapatro isn't there some simpler explanation for the same? – rahulserver Jun 03 '15 at 20:14
  • 1
    The accepted answer from the link in my previous comment has a simpler explanation. "Inversion of Control" is the key concept to understand. Grails service is a singleton bean and the container wires it up properly by taking the control from the user hence avoiding the need to instantiate a bean, – dmahapatro Jun 03 '15 at 20:22
  • @dmahapatro Thanks man! Enjoy another upvote ;) – rahulserver Jun 03 '15 at 20:28

0 Answers0