I implemented something like this:
@Path("/svc")
public class Service {
Resource rsc = Resource.getInstance();
@GET
public String doGet() {...}
}
public class Resource {
public static Resource instance;
private Resource() {...}
public static getInstance(){
if (instance == null){
return new Resource();
}
return instance;
}
}
Service
class is the where the GET
and POST
methods are implemented, where Resource
is the singleton class where some data is temporarily stored.
However, as I tested it, I found that the singleton class gets a new instance every time a method is called. The singleton class is just a classic Java singleton implementation. I know that adding the @Singleton
annotation fixes the problem, but I was wondering what caused this behavior?