泽西岛和 HK2 服务定位器 [英] Jersey and HK2 ServiceLocator

查看:29
本文介绍了泽西岛和 HK2 服务定位器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Application 构造函数(从 ResourceConfig 继承的东西)中初始化我的 Jersey 应用程序中的一些组件.看起来是这样的

I'm trying to initialize some components in my Jersey application in the Application constructor (the thing that inherits from ResourceConfig) . It looks like this

public Application(@Context ServletContext context,
                   @Context ServiceLocator locator)...

当我尝试在任何时候使用定位器时,我仍然无法使用 locator.create(MyThing.class) 方法创建已在 AbstractBinder 中注册的事物的实例.

When I try to use the locator at any point, I still can't create instances of things that I have registered in an AbstractBinder using the locator.create(MyThing.class) method.

我确定它们已正确绑定,因为它们已通过 @inject 字段注释正确注入到我的资源类中.

I'm certain that they are bound correctly because they are injected properly into my resource classes via the @inject field annotation.

不同之处在于 Jersey/HK2 框架正在实例化我的资源类(正如预期的那样,因为它们在我的包扫描路径中),但我似乎无法通过代码利用 ServiceLocator.

The difference is that the Jersey/HK2 framework is instantiating my resource classes (as expected, since they're in my package scan path), but I can not seem to leverage the ServiceLocator through code.

我的最终目标是在具有@Inject 属性时注入其他非球衣类,例如.我有一个需要注入配置的数据库访问层的工作类.我想说

My ultimate goal is to have other non-jersey classes injected when they have the @Inject attribute, eg. I have a worker class that needs to be injected with the configured database access layer. I want to say

locator.Create(AWorker.class) 

然后注入.

如何获得真正的 ServiceLocator,它将注入我已经注册/绑定到我的 Binder 的所有内容?(或者我应该使用 ServiceLocator 以外的东西吗?)

How do I get the real ServiceLocator that will inject everything I've already registered/bound with my Binder? (Or should I be using something other than ServiceLocator?)

推荐答案

你是如何启动你的容器的?如果您使用的是 ApplicationHandler,您只需调用:handler.getServiceLocator().ServiceLocator 确实是您想要用来访问依赖项的工具.

How are you starting up your container? If you are using ApplicationHandler, you can just call:handler.getServiceLocator(). The ServiceLocator is, indeed, what you want to be using to access your dependencies.

如果您正在启动一个 servlet,我发现访问服务定位器的最佳方法是在我的启动类中设置 Jersey 功能:

If you are starting up a servlet, I found that the best way to get access to the service locator was to have a Jersey feature set it on my startup class:

    private static final class LocatorSetFeature implements Feature {

    private final ServiceLocator scopedLocator;

    @Inject
    private LocatorSetFeature(ServiceLocator scopedLocator) {
        this.scopedLocator = scopedLocator;
    }

    @Override
    public boolean configure(FeatureContext context) {
        locator = this.scopedLocator; // this would set our member locator variable
        return true;
    }
}

该功能只需使用 config.register(new LocatorSetFeature()) 注册到我们的资源配置中.

The feature would just be registered with our resource config with config.register(new LocatorSetFeature()).

根据容器的生命周期绑定其他组件的启动很重要,所以这仍然感觉有点hacky.您可以考虑将这些类作为第一类依赖项添加到 HK2 容器中,然后简单地将适当的依赖项注入到您的第三方类中(例如,使用 Binder).

It would be important to tie in startup of other components based on the lifecycle of your container, so this still feels a bit hacky. You might consider adding those classes as first class dependencies in the HK2 container and simply injecting the appropriate dependencies into your third party classes (using a Binder, for example).

这篇关于泽西岛和 HK2 服务定位器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆