1

I am having two dropwizard java App in two module. Let's Say A and B,and Our B Module depends on A.

Now I want to inject different client in two different situations i.e one when A's dropwizard run and other when B's dropwizard run.

Suppose Myclass is in A module :

public class MyClass
{
  @Inject
  private final CustomClient client;

}

I want to do this with HK2 dependency injection. Notice class Myclass is not a resource class...

Currently, I have registered another Implementation for CustomClient in my App of module B like this :

environment.jersey().register(new AbstractBinder() {
      @Override
      protected void configure() {
        ...
        bind(new MyCustomClient()).to(CustomClient.class);
        ...
      }
    });

But when I run the app of B, and calling class MyClass of A from it I am getting null for CustomClient of Class Myclass in module A.

But I was thinking I will get an instance of MyCustomClient after getting injected.

How can I achieve what I want with HK2 DI?

Note: I want to do the injection in normal java class with HK2.

am10
  • 449
  • 1
  • 6
  • 17
  • I highly suggest you create a third module for code that is used by both app modules. Put the client into that third module, create a `Feature` that registers the `AbstractBinder`. The two modules will just need to register the `Feature`. – Paul Samsotha Jul 01 '18 at 03:17
  • Will do that If I don't find any other way... – am10 Jul 01 '18 at 05:05
  • 1
    I don't think I understand the use case well enough. How are you creating your "normal" class? If you are creating it yourself you can cause hk2 injection using https://javaee.github.io/hk2/apidocs/org/glassfish/hk2/api/ServiceLocator.html#inject-java.lang.Object- otherwise you would also have to register your "normal" class with hk2 and have hk2 create it by doing a lookup (or putting your normal class in a scope that would have hk2 automatically create it) – jwells131313 Jul 01 '18 at 13:08
  • I tried serviceLocator to inject but the issue with this is I need to have the instance of service locator everytime to Inject.It doesn't work like @inject but it works like servicelocator.inject(object). Actually i want to do injection with annotation. – am10 Jul 01 '18 at 13:12
  • @jwells131313 it would be good if you can give me a little example of this. Actually, wherever I found hk2 they try to do it in the resource class of dropwizard. I want this injection in any class. – am10 Jul 01 '18 at 13:15
  • Normal class is any external class used by dropwizard.. – am10 Jul 01 '18 at 13:30
  • 1
    You need to bind the class into the DI container if you want to inject other services into it. This is the basics of how [IoC](https://en.wikipedia.org/wiki/Inversion_of_control) works. You can't just manually instantiate arbitrary objects and expect services to magically be injected. You should bind all services and inject all services that you want to go through the DI system. Otherwise, as you mentioned, you need to ma manually inject yourself with the ServiceLocator. My first comment is simply a design recommendation, not a solution. – Paul Samsotha Jul 01 '18 at 19:00
  • Possible duplicate of [Dropwizard HK2 injection](https://stackoverflow.com/questions/48662531/dropwizard-hk2-injection) – minisu Oct 03 '18 at 06:58

0 Answers0