HK2注释未处理 [英] HK2 annotations not being handled

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

问题描述

我正在通过泽西岛使用HK2,希望可以使用@Immediate服务.在此过程中,我注意到(看起来)我的服务的任何注释都未注册.例如,在旋转我的ServiceLocator并查看我的描述符中带有@Singleton注释的服务后,它仍设置为@PerLookup.我用于启动应用程序处理程序的代码如下:

I'm using HK2 through Jersey, and am looking to get @Immediate services working. In the process, I notice that (as it seems) none of the annotations for my services are getting registered. E.g., after spinning up my ServiceLocator and looking at my descriptor for a service annotated with @Singleton, it is still set as @PerLookup. My code for initiating my application handler is below:

ApplicationHandler handler = new ApplicationHandler(resourceConfig, new AbstractBinder() { ... });

我的活页夹像这样注册服务:

My binder registers a service like so:

bindAsContract(ShouldHaveSingletonScope.class);

此后立即查看我的ServiceLocator,我发现该作用域没有从类中拉出(仍然是@PerLookup).我还需要指定其他内容来告诉HK2解析类注释吗?这似乎是一个非常标准的用例,所以我必须缺少一些东西.

Looking at my ServiceLocator immediately after this, I see that the scope is not being pulled from the class (still @PerLookup). Is there something additional I need to specify to tell HK2 to parse class annotations? This seems like a pretty standard use case so I must be missing something.

推荐答案

ServiceLocatorUtilities上有一种称为

There is a method on ServiceLocatorUtilities called addClasses

因此请访问ServiceLocator并执行

So get access to the ServiceLocator and just do

ServiceLocatorUtilities.addClasses(locator, ShouldHaveSingletonScope.class);

活页夹非常直白,只会照你说的做,而不是直接看课.

The binders are very literal, and will only do what you tell it as opposed to looking at the classes directly.

这篇关于HK2注释未处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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