如何在Jersey2 ServletContainer中获取HK2 ServiceLocator? [英] How to get HK2 ServiceLocator in a Jersey2 ServletContainer?

查看:245
本文介绍了如何在Jersey2 ServletContainer中获取HK2 ServiceLocator?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让Jersey2和Guice一起合作,这显然相当困难.我已经看到了一些解决方案,其中包括使用HK2到Guice桥.但是网桥依赖于在自定义Jersey2 ServletContainerinit()中获取HK2 ServiceLocator实例,以初始化GuiceBrige:

I'd like to get Jersey2 and Guice to cooperate together, which is apparently rather difficult. I've seen some solution to this involving using the HK2-to-Guice bridge. But the bridge rely on getting the HK2 ServiceLocator instance in the init() of a custom Jersey2 ServletContainer in order to initialize GuiceBrige:

public class MyServletContainer extends ServletContainer {
  @Override public void init() {
    ServiceLocator sloc = getApplicationHandler().getServiceLocator();
    ...
} }

但是在最新版本的Jersey(2.26)中,getServiceLocator()不再存在于ApplicationHandler中.在这种情况下如何获得它?

But somehow in the latests version of Jersey (2.26), the getServiceLocator() does not longer exists in ApplicationHandler. How can I get it in this context?

推荐答案

免责声明:我不使用Guice,所以这不是我测试过的东西.因此,我不知道OP尝试执行的操作是否还能正常工作.我只是在回答如何获取ServiceLocator的主要问题.

Disclaimer: I don't use Guice, so this is not something that I have tested. So I don't know if what the OP is trying to do will even work. I am simply answering the main question of how to get the ServiceLocator.

我的评论中提到这里,从2.26开始,泽西岛不再对HK2具有 hard 依赖.因此,在整个代码库中,您将不再看到对ServiceLocator的引用,而是看到了更高级别的InjectionManager. InjectionManager具有与ServiceLocator相同的目的,但是抽象允许依赖项注入提供程序的不同实现.这就是为什么在使用2.26时我们需要添加jersey-hk2依赖项的原因.这是InjectionManager的HK2实现.在此实现中,InjectionManager将在适当的地方简单地将调用委派给基础ServiceLocator.

As mentioned in my comment here, starting 2.26, Jersey no longer has a hard dependency on HK2. So throughout its codebase, you will no longer see a reference to a ServiceLocator, but instead, a higher level InjectionManager. The InjectionManager has the same purpose as the ServiceLocator, but the abstraction allows for different implementations of the dependency injection provider. This is why, when using 2.26, we need to add the jersey-hk2 dependency. This is the HK2 implementation of the InjectionManager. In this implementation, the InjectionManager will simply delegate calls to the underlying ServiceLocator where appropriate.

话虽这么说,ApplicationHandler使您现在可以访问InjectionManager,而不是ServiceLocator. ServiceLocator本身是一项服务,因此,如果您有定位器,则可以执行以下操作(这毫无意义,但这只是我的观点)

That being said, the ApplicationHandler gives you access to the InjectionManager now, rather than the ServiceLocator. The ServiceLocator itself is a service, so if you have a locator you could do the following (which is pointless, but it just shows my point)

ServiceLocator locator = getServiceLocator();
locator = locator.getService(ServiceLocator.class);

这意味着您还可以从InjectionManager获取定位器,这只是基础定位器的高级委托人

This means that you can also get the locator from the InjectionManager, which is just a high level delegator for the underlying locator

InjectionManager im = getApplicationHandler().getInjectionManager();
ServiceLocator locator = im.getInstance(ServiceLocator.class);

需要指出的一件事,而我免责声明的主要原因是您需要先在init()方法中调用super.init() ,否则您将获得NPE当您尝试获取ApplicationHandler时.问题是初始化完成了 alot .几乎整个应用程序都已初始化.因此,尝试添加Guice集成可能为时已晚,也可能为时不晚.

One thing that needs to be pointed out, and the main reason for my disclaimer is that you need to call super.init() first in your init() method, or else you will get an NPE when you try to get the ApplicationHandler. The problem with this is that there is alot of initialization done; pretty much the entire application is initialized. So it may or may not be too late to try and add your Guice integration.

在其他一些地方,我看到了此集成.而且我相信,在您尝试在init()末尾这样做之前,它们都会被击中.

Here are some other places where I have seen this integration done. And I believe they would all be hit before you trying to do it at the end of the init().

  • ResourceConfig构造函数中,您可以在其中注入InjectionManager.
  • Feature中,您可以通过InjectionManagerProvider静态方法获得InjectionManager.
  • 我还没有看到任何实现,但是我认为进行桥接的首选位置在ComponentProvider中,如球衣-弹簧4 .这可能需要更多的工作,但我认为这将是最合适的位置,因为在所有其他之前的选项之前都提到了它.不过,由于我已经看到其他人摆脱了另两个选择,因此可能并不需要这样做.
  • In a ResourceConfig constructor, where you could inject the InjectionManager.
  • In a Feature where you could get the InjectionManager through the InjectionManagerProvider static method.
  • I have not seen any implementation of this, but I think the preferred location to do the bridge would be in a ComponentProvider, as mentioned in the docs. The only implementation I have seen is for Spring. You can see the source in the jersey-spring4. This is probably requires more work, but I think it would be the most appropriate location, as it is called before all the other previous options. It may not be required though, as I've seen others get away with the other two options.

这篇关于如何在Jersey2 ServletContainer中获取HK2 ServiceLocator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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