我试图使用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