When to use @Singleton annotation of Jersey? -
By default Jersey creates a new instance of the resource class for every request. So if you don't annotate the Jersey resource class, it implicitly uses
@RequestScoped
scope.
@RequestScoped
endpoint looks more suitable for various reasons. It is stateless, provides new instance for each request. I have implemented DAO's based on http://www.benmccann.com/hibernate-with-jpa-annotations-and-guice example so provided EntityManager
objects are different only in @RequestScoped
environment because they are received from ThreadLocal<EntityManager>
cache (Jersey, Guice and Hibernate - EntityManager thread safety).
On the other hand, I have faced applications where Jersey endpoints are annotated as @Singleton
. But it seems that removing annotation won't change application behavior/logic.
When @Singleton
should be used instead of default @RequestScoped
for REST endpoint?