Jersey HK2 Dependency Injection doesn't work after update to v2.27

泪湿孤枕 提交于 2019-12-23 02:53:29

问题


I have a project using Jersey v2.25.1. I was using Jersey's inbuilt HK2 injection to perform dependency injection, and everything worked fine. Fast forward to now, I decided to update to Jersey v2.27.

When I ran my project, I got the following exception:

java.lang.IllegalStateException: InjectionManagerFactory not found

After some googling, I found that I needed to add the jersey-hk2 dependency. Doing so made me get the following exception:

org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=<MyClass>,parent=<MyClass>,qualifiers={},position=0,optional=false,self=false,unqualified=null,1044705957)

Upon reverting all my dependencies to Jersey v2.25.1, everything works fine. What do I need to do to fix these errors, so I can use Jersey v2.27?

Edit: I don't use Maven so I can't really post a pom.xml, but tommorow I will put together an MVCE with the exact dependencies I have, and a basic example of Dependency Injection.


回答1:


Answer by Paul Samsotha in a comment:

Try to change your AbstractBinder import. There are two, a Jersey one and an HK2 one. Try to use the Jersey one.

Basically, I needed to change the AbstractBinder class I implemented from

org.glassfish.hk2.utilities.binding.AbstractBinder

to

org.glassfish.jersey.internal.inject.AbstractBinder

The difference is that Jersey decoupled HK2 from it's internal DI mechanism in version 2.26, and thus, I needed to use the new AbstractBinder import, which comes directly from Jersey, and not HK2.

There are a few API differences: for instance, instead of a Factory<T>, bindFactory() takes a java.util.function.Supplier<T>.



来源:https://stackoverflow.com/questions/51217120/jersey-hk2-dependency-injection-doesnt-work-after-update-to-v2-27

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!