I am running RESTEasy in an embedded web server (Jetty).
My resources need access to a backing data store, configuration of which is passed in on the command line that is used to start our application.
Ideally, I'd inject the backing data store resource in the constructor of the resource:
@Path("/")
public class MappingService{
private final RecordManager recman;
public MappingService(RecordManager recman) {
this.recman = recman;
}
@PUT
@Path("/mapping/{key}")
@Produces({MediaType.APPLICATION_JSON, "application/*+json"})
public Response createMapping(@PathParam("key") String key, @QueryParam("val") String val) {
// do stuff with recman ...
}
}
Ideally, I'd configure the RecordManager object elsewhere in the application, then make it available to the MappingService constructor.
I see that I can use an Application object to return specific singleton instances. But it looks like RESTEasy constructs the Application object itself - so I'm not seeing any way to pass configuration objects into the Application singleton.
So: How do I pass an externally instantiated object into my resource handler?
I found this article ( Share variables between JAX-RS requests ) that describes how to use a context listener to add an object to the context and pull it inside the resource handler - but this is horrible - I have to take what should be a POJO and suddenly I've made it highly dependent on it's container. Please tell me there is a better way!