我試圖使用vertx-jersey創建一個web服務,我可以在其中注入自己的自定義服務以及一些更多的標準對象,如vertx
實例本身。Vertx + Jersey + HK2:使用@Contract和@Service的ServiceLocator自動綁定
在我初始化的網絡服務器,像這樣的時刻(即以下this example):
Vertx vertx = Vertx.vertx();
vertx.runOnContext(aVoid -> {
JsonObject jerseyConfiguration = new JsonObject();
// ... populate the config with base path, resources, host, port, features, etc.
vertx.getOrCreateContext().config().put("jersey", jerseyConfiguration);
ServiceLocator locator = ServiceLocatorUtilities.bind(new HK2JerseyBinder());
JerseyServer server = locator.getService(JerseyServer.class);
server.start();
});
我遇到的問題是,我也希望能夠利用依賴注入的,所以我可以自動使用@Contract
和@Service
HK2
註釋來連接我的其他服務。
的問題是,我使用的ServiceLocatorUtilities
中,我明確地結合HK2JerseyBinder
和我的理解它,我應該只建立一個單一ServiceLocator
實例中,一切都應該是可訪問/必然會已經創建ServiceLocator
。
我也知道,我可以打電話給ServiceLocatorUtilities.createAndPopulateServiceLocator()
代替,但它看起來像JerseyServer
與一切在HK2JerseyBinder
勢必沿將被錯過了,因爲他們沒有註釋。
有沒有一種方法可以解決這個問題?
每個ServiceLocator都有一個綁定到其中的DynamicConfigurationService(https://javaee.github.io/hk2/apidocs/org/glassfish/hk2/api/DynamicConfigurationService.html)。從中你可以得到一個Populator(https://javaee.github.io/hk2/apidocs/org/glassfish/hk2/api/Populator.html)。 populator的方法可以用來動態地添加(例如)居民文件或類掃描的服務,或者從您希望用來發現服務的其他自動機制中動態添加服務。 – jwells131313