0

What actually happens when we do def someService? Does the service code get linked to the controller code?

tim_yates
  • 167,322
  • 27
  • 342
  • 338
Alexander Suraphel
  • 10,103
  • 10
  • 55
  • 90

3 Answers3

1

Grails uses spring IOC, your controllers and services are managed as spring beans, when you define a service inside a controller, spring will inject the service inside the controller, code does not get linked in anyway, just reference to service will be set. Though its not a much expensive operation, you would not want to define service dependencies that are not used to keep the code clean

Sudhir N
  • 4,008
  • 1
  • 22
  • 32
1

I think under the hood it's the same process as Spring's @Autowired annotation, so you pay a bit of a performance penalty on start up but I don't think it's significant.

There's another stackoverflow question on the subject here.

Community
  • 1
  • 1
rcgeorge23
  • 3,594
  • 4
  • 29
  • 54
0

Does the service code get linked to the controller code?

That does not make sense.

Actually services in grails are singleton by default.So if you inject a Service by def serviceName it wont create a new service object but a reference to same old service object.
So its not expensive of course.

But if in a service there is a property static session="prototype" or some non-singleton like this.Then it is expensive .

Sachin Verma
  • 3,712
  • 10
  • 41
  • 74