Using jersey's HK2 to inject healthchecks, lifecycle Manageds etc.
See original GitHub issue(I have asked this on google dropwizard-user groups)[https://groups.google.com/forum/#!topic/dropwizard-user/dmT8BGi9Wcc] and couldn’t find a solution. To me it looks like it should be supported given the jersey interface provided by dropwizard.
Let’s say I’m using HK2 for dependency injection throughout the application. That’s what ´environment.jersey().register(MyResource.class) does after all.
I bind my service classes via AbstractBinder, while resource classes via directly registering to jersey.
environment.jersey().register(new AbstractBinder() {
@Override
protected void configure() {
...
bind(MyService.class):
...
}
});
environment.jersey().register(MyResource.class); //MyResource uses MyService
so my resources etc. can get their dependencies via @Inject.
There are some cases where our healthchecks and Lifecycle classes require those singleton service dependencies (MyService). Even without those specific dependencies, I’d prefer to stick to using dependency injection throughout the application.
However, environment.healthchecks().register(myHealthCheckObject) //Healtcheck also uses MyService and environment.lifecycle().register(myManagedObject) only accept instances, not classes; which means they are not injected through the DI framework.
I don’t have access to the ServiceLocator of jersey, hence cannot create an instance of Healthcheck. I couldn’t find a way to access jersey’s ServiceLocator, probably also because it’s not initialized until after Application.run() is run.
Nor I could environment.lifecycle().register(MyManaged.class), which I think should be possible and is the aim of this issue.
I understand dropwizard is in a different context than jersey, and isn’t utilizing hk2 directly itself. But this limits our dependency capabilities entirely.
Creating my own ServiceLocator, and handling DI through that is an option but will fail when I start implementing my own AuthFactoryProvider because it requires dependencies from Jersey. Also I won’t be able to ´environment.jersey().register(MyResource.class)` directly.
Issue Analytics
- State:
- Created 9 years ago
- Comments:14 (6 by maintainers)

Top Related StackOverflow Question
(I agree – the HK2 DI stuff has been “enterprised” to the point where it is impossible to understand and more of a hindrance than a help. The rest of Dropwizard works quite well though; I prefer it to Spring, which has plenty of its own over-engineering issues.)
Looks like I have forgotten to close this issue. If you check my other issue #1026, I have found a solution to that. But yeah, HK2 is quite difficult to work with given the lack of documentation.
So, either you create your own service locator instead of registering binders to Jersey, and only register your ServiceLocator
or you “bridge” your service locator: https://github.com/natnan/dropwizard-executor-test/blob/master/src/main/java/com/natnan/dropwizard/executor/ExecutorApplicationBridge.java