2
我到處搜索了一個關於如何在Jersey 2.0中使用HK2依賴注入的基本示例,但總結得不多。你如何在Jersey 2.0上使用HK2依賴注入?
從this question,看起來你需要創建一個類延伸AbstractBinder
。但是,該示例的其餘部分顯示瞭如何通過編輯web.xml文件嚮應用程序註冊活頁夾。但是,我想避免這種情況,並希望直接在我的HttpServer
實例中註冊活頁夾。
這是我爲我的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");
任何幫助將不勝感激。
感謝您的信息!你見過這個https://github.com/jersey/jersey/pull/15(它增加了對澤西島的支持) –
太棒了!我得看看這個。我對hk2感到有些沮喪。 –