As I've been struggling to migrate our micro-services to 0.8.0-rc2, I've decided to (or am sort of forced to) migrate to HK2 from Guice.
Among a few challenges, there is one about dropwizard directly. I've been binding classes using
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. Even without those specific dependencies, I'd prefer to stick with using dependency injection throughout the application..
However,
environment.healthchecks().register(HealthCheck) //Healtcheck also uses MyService
and
environment.lifecycle().register(Managed)
accept instances, not classes; which means they are not injected through the DI framework.
With guice, I had access to the injector, hence I was handling it via
injector.getInstance(...)
But now 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.
I understand dropwizard is in a different context than jersey, and isn't utilizing hk2 directly itself. But this limits our dependency capabilities entirely. Unless I've misunderstood it all wrong :).
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 (that's actually why we decided to migrate to HK2 in the end).
Any ideas how I can handle dependency injection on healthchecks etc.?