I have a resource to be exposed as Restful WS.If I have to make it Singleton, what is the preferred and advised way:
1 . Annotating the resource class using @Singleton
Or
2 . Implementing the getSingletons()
method in my Application class implementation and instantiating the resource there like
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 both ways and realised that both of them creates a singleton instance of the resource class, PlayerResource in this case.