question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 9 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
RichardBradleycommented, Mar 11, 2016

(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.)

3reactions
natnancommented, Nov 16, 2015

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

// create your binders here

// random UUID is added for random locator name generation so DropwizardAppRules wouldn't fail
serviceLocator = ServiceLocatorUtilities.bind(UUID.randomUUID().toString(), binders.toArray(new Binder[binders.size()]));
    environment.getApplicationContext().getAttributes().setAttribute(ServletProperties.SERVICE_LOCATOR, serviceLocator);

// create your healtchecks here.

or you “bridge” your service locator: https://github.com/natnan/dropwizard-executor-test/blob/master/src/main/java/com/natnan/dropwizard/executor/ExecutorApplicationBridge.java

Read more comments on GitHub >

github_iconTop Results From Across the Web

dropwizard 0.8.0-rc2, using jersey's HK2 to inject ...
so my resources etc. can get their dependencies via @Inject. There are some cases where our healthchecks and Lifecycle classes require those singleton ......
Read more >
Chapter 24. Custom Injection and Lifecycle Management
Implement your own HK2 Factory to provide the injectable instances. · Use the HK2 Factory to define an injection binding for the injected...
Read more >
Jersey HK2 : Dependency injection and healthchecks
I am looking to implement Healthchecks in these process so that I can see if the process has come up fine after deployment...
Read more >
Implementing Custom Injection Provider - Jersey En - CWIKI.US
Implement your own HK2 Factory to provide the injectable instances. Use the HK2 Factory to define an injection binding for the injected instance ......
Read more >
Overriding Dependency Injection with HK2 in Jersey Tests
A common problem when testing APIs is to be able to inject Mock of the services we don't want to invoke during our...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found