简体   繁体   中英

Why Grails Service Class Is Singleton?

I came to know that Grails service class are of Singleton type. For what reason, the service classes are defined to be Singleton?

Thanks in advance.

Grails services may be used with different scopes , not just singleton , by adding something like this to the class:

static scope = "flow"

From the manual:

  • prototype - A new service is created every time it is injected into another class
  • request - A new service will be created per request
  • flash - A new service will be created for the current and next request only
  • flow - In web flows the service will exist for the scope of the flow
  • conversation - In web flows the service will exist for the scope of the conversation. ie a root flow and its sub flows
  • session - A service is created for the scope of a user session
  • singleton (default) - Only one instance of the service ever exists

The main reason for choosing singleton as the default is for better performance, both in reduced memory usage (only one instance is sitting around), and in reduced processing time, because you eliminate the overhead of creating a new object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM