Search code examples
parentinstantiationinjectdescriptorhk2

HK2 InstantiationService nested inject


I m using hk2 as CDI engine. I have 2 nested injection as in the code below:

public class Root {
@Inject
Son son;
 ...
}

public class Son {
@Inject
GrandSon gs; //should i put it here? 
  ...
}

public class GrandSon {

  ...
}

These are the Factory classes:

public class SonFactory implements Factory<Son>{
    @Inject
    InstantionService is;

    @Inject
    GrandSon gs; //should i put it here? 

    public Son provide(){
      is.getInstantiationData()
      return sonImpl;
    }

    public dispose(Son instance){
   // destroy
    }

}

public GrandsonFactory implements Factory <GrandSon>{
    @Inject
    InstantionService is

    public GrandSon provide(){
      is.getInstantiationData()
      return sonImple;
    }

    public dispose(GrandSon instance){
   // destroy
    }
}

i bound both factory as: bindFactory(SonFactory.class).to(Son.class).in(RequestScoped.class) bindFactory(GrandSonFactory.class).to(GrandSon.class).in(RequestScoped.class)

Now i want just using the InstantionService.getInstantiationData() to get descriptor data from the calling parent inside the GrandSon class. In particular i need to rise back till to the calling Root class inspecting the injectee parent. I can get data from the factory.provide method of Son class, but i cannot get a valid getInstantiationdata() from grandSon class. What am i wrong with code?


Solution

  • This seems to be a bug in HK2. I have entered the following JIRA:

    Nested Factory Instantation Service Issue

    I'll update this answer once this bug gets fixed. I have checked in a failing test demonstrating the issue