1

My project, project, used to use HK2 for DI, and now I'm trying to migrate it to using dagger2. It depends on a library, lib, which uses HK2 for DI. Dependencies are managed with Gradle.

One of my classes looks like:

package com.mine;

import lib.LibService;
import lib.LibType;

public class MyClass {
    private final LibService<LibType> service;

    @Inject
    public MyClass(LibService<LibType> service) {
        this.service = service;
    }

    //...
}

When I try to run the code, I get an error caused by HK2: There was no object available for injection at SystemInjecteeImpl(requiredType=LibService<LibType>,parent=MyClass,...). Stepping through the debugger, I see that HK2 is processing the @Inject annotation, instead of Dagger2. It's likely that my Dagger2 Modules, Components, etc. are not exactly right, and I need to fix that. Regardless, I do not want HK2 "processing" the @Inject on the constructor of MyClass. To make things harder, there are many other transitive dependencies from lib that I want, such as javax.ws.rs.POST.

I tried removing HK2 from my dependencies, but this causes an error Caused by: java.lang.ClassNotFoundException: org.glassfish.hk2.utilities.binding.BindingBuilder from inside lib.

configurations.all {
    exclude group: 'org.glassfish.hk2'
}

How can I set up Gradle to use hk2 for lib, but NOT for MyClass or other things in project, while including other transitive dependencies from lib that I want?

PunDefeated
  • 566
  • 5
  • 18
  • hk2 only "processes" classes if it has been told about them somehow. How does hk2 even know about your class? – jwells131313 Feb 28 '20 at 13:32
  • @jwells131313 that's a good question, I have no idea. I don't have a binder in my code. Maybe something in the dependency is causing it to happen. – PunDefeated Feb 28 '20 at 17:24
  • ok, when you run set this property "org.jvnet.hk2.properties.bind.tracing.pattern" to * . The output of that should tell you who is registering your service with hk2. You can also set the property "org.jvnet.hk2.properties.bind.tracing.stacks" to true. That'll print stack traces whenever anything is bound. That'll tell you who is registering your code with hk2 – jwells131313 Mar 02 '20 at 12:29

0 Answers0