如何在Jersey 2.12获得HK2 ServiceLocator? [英] How to get HK2 ServiceLocator in Jersey 2.12?
问题描述
我想创建一个类的单例实例,该类没有作为资源或服务参与Jersey,但希望从Jersey ServiceLocator注入其依赖项。
I would like to create a singleton instance of a class that is not involved in Jersey as a Resource or Service and yet would like its dependencies injected from the Jersey ServiceLocator.
我可以在我的ResourceConfig构造函数中手动注册这个类,然后将ResourceConfig传递给Grizzly工厂方法,如下所示:
I can register this class manually in my ResourceConfig constructor, the ResourceConfig is then passed in to the Grizzly factory method like so:
ResourceConfig resourceConfig = new DeviceServiceApplication();
LOGGER.info("Starting grizzly2...");
return GrizzlyHttpServerFactory.createHttpServer(BASE_URI,
resourceConfig, mServiceLocator);
剩下的问题是如何获得对Jersey ServiceLocator的引用,以便我可以调用createAndInitialize()获取我的对象注入依赖项。我在以前的Jersey版本中看到有一些构造函数变体需要ApplicationHandler,它当然提供对服务定位器的访问(我如何初始化这是另一回事)。您还可以看到我已经尝试传入父ServiceLocator,但当然解决方案是从child - > parent locator而不是在另一个方向,所以要求父对象失败因为Jersey @Contract和@Service类型不是这里可以看到。
The problem that remains is how to get a reference to the Jersey ServiceLocator so I can call createAndInitialize() to get my object with dependencies injected. I see in previous Jersey versions there were constructor variants that expect an ApplicationHandler, which of course provides access to the service locator (how I initialise that is another matter). You can also see I have tried passing in a parent ServiceLocator but of course the resolution happens from child -> parent locator and not in the other direction so asking the parent for my object fails because the Jersey @Contract and @Service types aren't visible here.
我是否需要使用GrizzlyHttpServerFactory以外的东西?我是否放弃并手动连接我的单例依赖项?
Do I need to use something other than GrizzlyHttpServerFactory ? Do I give up and manually wire my singleton's dependencies?
推荐答案
我能够获得对的引用ServiceLocator
通过注册 ContainerLifecycleListener
。
I was able to get a reference to ServiceLocator
by registering a ContainerLifecycleListener
.
在 onStartup中(容器容器)
方法,调用 container.getApplicationHandler()。getServiceLocator()
。
此示例将引用存储为 ResourceConfig
的成员变量,您可以通过访问者在其他地方使用该参数。
This example stores the reference as a member variable of ResourceConfig
which you can use elsewhere via an accessor.
class MyResourceConfig extends ResourceConfig
{
// won't be initialized until onStartup()
ServiceLocator serviceLocator;
public MyResourceConfig()
{
register(new ContainerLifecycleListener()
{
public void onStartup(Container container)
{
// access the ServiceLocator here
serviceLocator = container.getApplicationHandler().getServiceLocator();
// ... do what you need with ServiceLocator ...
MyService service = serviceLocator.createAndInitialize(MyService.class);
}
public void onReload(Container container) {/*...*/}
public void onShutdown(Container container) {/*...*/}
});
}
public ServiceLocator getServiceLocator()
{
return serviceLocator;
}
}
然后在其他地方:
MyService service
= myResourceConfig.getServiceLocator().createAndInitialize(MyService.class);
这篇关于如何在Jersey 2.12获得HK2 ServiceLocator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!