0

we have implemented a framework that uses hk2 with custom annotations to inject objects in our rest services classes. Now we want to inject objects also if the annotations are present inside the injected objects in a recoursive way. Is there a clean way to do this with hk2? or must I use another library? in this case, which library can solve this task (we need a library as light as possible) ? Thank you for reply

UPDATE

The framework we have implemented is very similar to the one explained by @peeskillet in this post. Follows the explanation of my needs:

For example I have a service that use @myCustomAnnotation like this:

@Path("/test")
public class RestService implements IRestService {
   @myCustomAnnotation 
   private TestObject object;

   @GET
   @Path("/myTestObject")
   public TestObject getMyObject(){
       return object;
   }
}

All works fine.

My problem is this:

If I have into the TestObject class definition, another object that uses @myCustomAnnotation, such as:

public class TestObject {
     @myCustomAnnotation
     private AnotherObject object;

     public AnotherObject getObject(){
        return object;
     }
}

it isn't injected, so the AnotherObject object is always null.

I have, into the resolve method of the InjectionResolver, the resolution of the 2 kind of objects:

public Object resolve(Injectee injectee, ServiceHandle<?> handle) {

    Object result=null;
    if (TestObject.class == injectee.getRequiredType()) {
        result = systemInjectionResolver.resolve(injectee, handle);
    } else if (AnotherObject.class == injectee.getRequiredType()) {
        result = systemInjectionResolver.resolve(injectee, handle);
    } 
    return result;
}

I hope I was clear.

Community
  • 1
  • 1
Mara
  • 41
  • 3
  • 2
    Do you have some code we can test that reproduces the problem? Please post it for better help. – Paul Samsotha May 25 '16 at 08:45
  • A agree with peeskillet, I don't exactly understand the issue that you are having – jwells131313 May 25 '16 at 12:09
  • yes, I will edit my post – Mara May 25 '16 at 12:15
  • 1
    [Works fine for me](https://gist.github.com/psamsotha/838a194f883438faee2037e2baabe6c6). If you are binding using an instance (i.e. instantiating the `TestObject`), the injection of the `AnotherObject` will not work. I would need to see your DI configuration if you still need more help. – Paul Samsotha May 25 '16 at 14:14
  • Sorry for my late response but I was busy with other activities. Yes you are right peeskillet, I have found the problem: currently I'm not working with bindAsContract into the Binder but with bindFactory and this seems to block deep injection. Do you know a method to use bindAsContrat with the factories to create the object? Thanks for reply – Mara Jun 06 '16 at 12:44
  • 1
    You can inject the `ServiceLocator` into the `Factory`. Then call `locator.inject(testObject)` inside the factory. This will explicitly inject the `testObject` with the `AnotherObject`. Have a look at [this post](http://stackoverflow.com/a/34653856/2587435) – Paul Samsotha Jun 06 '16 at 14:21
  • Thank you peeskillet, I have found exactly the same solution – Mara Jun 06 '16 at 15:51

0 Answers0