泽西岛和HK2 ServiceLocator [英] Jersey and HK2 ServiceLocator

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

问题描述

我试图在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,该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()).

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

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 ServiceLocator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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