What actually happens when we do def someService? Does the service code get linked to the controller code?
- 161,005
- 26
- 328
- 327
- 9,083
- 9
- 48
- 86
3 Answers
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
- 3,978
- 1
- 21
- 31
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.
- 1
- 1
- 3,454
- 4
- 27
- 52
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 .
- 3,544
- 9
- 35
- 70