13.2 Scoped Services - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari
Version: 3.1.9
13.2 Scoped Services
By default, access to service methods is not synchronised, so nothing prevents concurrent execution of those methods. In fact, because the service is a singleton and may be used concurrently, you should be very careful about storing state in a service. Or take the easy (and better) road and never store state in a service.You can change this behaviour by placing a service in a particular scope. The supported scopes are:prototype- A new service is created every time it is injected into another classrequest- A new service will be created per requestflash- A new service will be created for the current and next request onlyflow- In web flows the service will exist for the scope of the flowconversation- In web flows the service will exist for the scope of the conversation. ie a root flow and its sub flowssession- A service is created for the scope of a user sessionsingleton(default) - Only one instance of the service ever exists
If your service isTo enable one of the scopes, add a static scope property to your class whose value is one of the above, for exampleflash,floworconversationscoped it must implementjava.io.Serializableand can only be used in the context of a Web Flow.
static scope = "flow"
Upgrade note: Starting with Grails 2.3, new applications are generated with configuration that defaults the scope of controllers tosingleton. Ifsingletoncontrollers interact withprototypescoped services, the services effectively behave as per-controller singletons. If non-singleton services are required, controller scope should be changed as well.See Controllers and Scopes in the user guide for more information.
