【问题标题】:Vertx + Jersey + HK2: ServiceLocator autobindings using @Contract and @ServiceVertx + Jersey + HK2:使用 @Contract 和 @Service 的 ServiceLocator 自动绑定
【发布时间】: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 中的所有其他内容都会被遗漏,因为它们没有注释。

有没有办法我可以同时做这两个或解决这个问题?

【问题讨论】:

标签: java dependency-injection jersey vert.x hk2


【解决方案1】:

扩展 jwelll 的评论:

ServiceLocatorUtilities 正是它的名字所暗示的:它只是一个帮助创建ServiceLocator 的实用程序。要在没有实用程序的情况下创建它,您可以使用 ServiceLocatorFactory。当您调用它的一个创建函数时,这就是实用程序在后台所做的事情。

ServiceLocator locator = ServiceLocatorFactory().getInstance().create(null);

当你想动态添加服务到定位器时,你可以使用DynamicConfigurationService,这是默认提供的服务。

DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);

此服务有一个Populator,您可以获取并调用它populate 方法,以便它使用来自您的inhabitant files 的服务填充定位器(您可以使用generator 获取这些服务)。

Populator populator = dcs.getPopulator();
populator.populate();

这是所有ServiceLocatorUtilities.createAndPopulateServiceLocator() does

public static ServiceLocator createAndPopulateServiceLocator(String name) throws MultiException {
    ServiceLocator retVal = ServiceLocatorFactory.getInstance().create(name);

    DynamicConfigurationService dcs = retVal.getService(DynamicConfigurationService.class);
    Populator populator = dcs.getPopulator();

    try {
        populator.populate();
    }
    catch (IOException e) {
        throw new MultiException(e);
    }

    return retVal;
}

因此,既然您已经有了定位器的实例,您需要做的就是获取动态配置服务、获取填充器并调用其populate 方法。

ServiceLocator locator = ServiceLocatorUtilities.bind(new HK2JerseyBinder());
DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
Populator populator = dcs.getPopulator();
populator.populate();

如果你想反过来做(首先是填充器),你可以这样做

ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
ServiceLocatorUtilities.bind(locator, new HK2JerseyBinder()); 

在后台,实用程序将只使用动态配置服务。

有很多不同的方法可以(动态地)添加服务。欲了解更多信息,请查看the docs。另一个很好的信息来源是我链接到的ServiceLocatorUtilities 的源代码。

【讨论】:

    猜你喜欢
    • 2016-10-14
    • 2014-11-01
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2015-07-13
    • 2019-09-10
    相关资源
    最近更新 更多