@Singleton or getSingletons () when implementing Singleton Resources in Jersey

I have a resource that will display as Restful WS.If I need to make it Singleton, which is the preferred and recommended way:

1. @Singleton resource class using @Singleton

Or

2. Implementing the getSingletons() method in my application class implementation and instantiating the resource, for example

  public class RestApplication extends Application { private Set<Object> singletons = new HashSet<Object>(); public RestApplication() { singletons.add(new PlayerResource()); } @Override public Set<Class<?>> getClasses() { return null; } @Override public Set<Object> getSingletons() { return singletons; } } 

I tried in both directions and realized that both of them create in this case a singleton instance of the resource class, PlayerResource.

+6
source share

Source: https://habr.com/ru/post/891630/


All Articles