I have searched everywhere for a basic example of how to use HK2 dependency injection in Jersey 2.0, but have come up short.
From this question, it appears you need to create a class which extends AbstractBinder
. However, the rest of the example shows how to register the binder with your application by editing the web.xml file. However, I want to avoid this and would like to instead register the binder with my HttpServer
instance directly.
This is what I have written for my HttpServer:
int port = config.getInt("port", 8080);
boolean useFake = config.getBoolean("fake", false);
final URI baseUri = URI.create("http://" + "localhost" + ":" + port + "/");
List<Binder> binders = Arrays.asList((Binder)new StatusModule(useFake),
(Binder)new NetworkModule(useFake));
final ApplicationHandler applicationHandler = new ApplicationHandler();
applicationHandler.registerAdditionalBinders(binders);
WebappContext webappContext = new WebappContext("Webapp context", "/resources");
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(
baseUri, applicationHandler);
for(NetworkListener listener : server.getListeners()){
listener.setCompression("on");
}
server.getServerConfiguration().addHttpHandler(
new StaticHttpHandler("/jersey2app/www"), "/static");
Any help will be greatly appreciated.