Came along this question, because for the first time I had a use case for not using @Singleton annotation.
Singleton is a design pattern, you should use it if:
- The object you are "singletonizing" keeps a state that must be shared and kept unique (example: a global counter)
- Generally, I design REST API without keeping a state, everything is handled in the method (full closure): so generally all my resources are singletons (use case: better performance)
That said, today I found this use case for not using Singleton:
@Path("/someendpoint/{pathparam}/somethingelse/")
//@Singleton
public class MyResource {
@PathParam("pathparam")
private String pathparam;
}
Using this, I'm bounding the path param to my instance, so it must be RequestScoped.
Generally, I'd have put @PathParam annotation in every method, so @Singleton would have been right on the class.
I'm not sure about the performances however, creating and destroying an object isn't a free operation