Spring + Thread safe singletons
I'm working on a project where we use MULE and Spring. In the context we create beans that provide the services. All the beans a开发者_StackOverflowre essentially thread safe singletons. Is this a popular/recommended way of writing services?
By default a bean in spring will be a singleton and it is a very common scenario you describe.
Might be problematic performance wise. If you have many threads competing for the same service. The bean is defined thread safe, so acess from different threads would be effectively serialized.
In our RESTFul service we set up our entry points on a
@com.sun.jersey.spi.resource.PerRequest
basis and
@org.springframework.context.annotation.Scope("request")
which keeps our throughput up but we monitor to ensure that GC is good enough not to bloat the app.
Spring singletons are inherently thread-safe plus this is the default scope -- which performs quite well as we use them in all our web apps.
精彩评论