【发布时间】:2017-08-27 15:54:42
【问题描述】:
我正在尝试使用 vertx-jersey 创建一个 Web 服务,我可以在其中注入我自己的自定义服务以及一些更标准的对象,例如 vertx 实例本身。
目前我正在像这样初始化网络服务器(即关注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 @987654329 连接我的其他服务@注解。
问题是我已经使用ServiceLocatorUtilities 创建了ServiceLocator,我在其中显式绑定了HK2JerseyBinder,据我所知,我应该只创建一个ServiceLocator 实例,其中一切都应该是可访问/绑定。
我也知道我可以改为调用ServiceLocatorUtilities.createAndPopulateServiceLocator(),但是看起来JerseyServer 以及绑定在HK2JerseyBinder 中的所有其他内容都会被遗漏,因为它们没有注释。
有没有办法我可以同时做这两个或解决这个问题?
【问题讨论】:
-
每个 ServiceLocator 都绑定了一个 DynamicConfigurationService (javaee.github.io/hk2/apidocs/org/glassfish/hk2/api/…)。从中你可以得到一个 Populator (javaee.github.io/hk2/apidocs/org/glassfish/hk2/api/…)。填充器具有可用于从(例如)居民文件或类扫描或您想用于发现服务的任何其他自动机制动态添加服务的方法。
标签: java dependency-injection jersey vert.x hk2