3

I have been trying to inject a service to a contract:

@org.jvnet.hk2.annotations.Contract
public interface CategoryDAO{
}

@org.jvnet.hk2.annotations.Service
public class HibernateCategoryDAO implements CategoryDAO
}

@org.jvnet.hk2.annotations.Service
public class MyCategoryManager implements CategoryManager {

    @javax.inject.Inject
    CategoryDAO categoryDAO;

}

But categoryDAO is null.

I tried even to bind it:

public class ServiceBinder implements Binder {
    @Override
    public void bind(DynamicConfiguration config) {
    config.bind(BuilderHelper.link(HibernateCategoryDAO.class).to(CategoryDAO.class).build());
    }
}

But still it stays null.

I init the MyCategoryManager with Reflections framework like this:

Set<?> managers = Reflections.getSubTypesOf(CategoryManager.class);
Dejell
  • 13,947
  • 40
  • 146
  • 229
  • We got rid of the hk2 specific @Inject a while ago (when we changed to hk2 2.0). In what framework are you working? Like, where is your Binder given to hk2? Are you using https://hk2.java.net/2.3.0-b01/apidocs/org/glassfish/hk2/utilities/ServiceLocatorUtilities.html#bind%28org.glassfish.hk2.utilities.Binder...%29 ? You might want to also consider https://hk2.java.net/2.3.0-b01/apidocs/org/glassfish/hk2/utilities/ServiceLocatorUtilities.html#addClasses%28org.glassfish.hk2.api.ServiceLocator,%20java.lang.Class...%29 since you are annotating your classes – jwells131313 Feb 26 '14 at 16:55
  • I am using Jersey and the binder is being called in a class that extends ResourceConfig – Dejell Feb 26 '14 at 23:08
  • Truthfully I'm not all that familiar with how Jersey binds things. One thing to know is that when using Binders the annotations (like Contract and Service) on the classes generally don't mean anything. You might want to get the ServiceLocator and use the ServiceLocatorUtilities.addClasses method if you want it to automatically analyze your classes. However, I think it should still be honoring the Inject though, so I'm stumped as to why it would be null – jwells131313 Feb 27 '14 at 00:32
  • I wonder what happens if you print the stack trace from the constructor of HibernateCategoryDAO? Is it hk2 that is constructing this object? – jwells131313 Feb 27 '14 at 10:55
  • How does one print the stacktrace from the Constructor? – Dejell Feb 27 '14 at 10:57
  • I mean to just create a no-args constructor for HibernateCategoryDAO with Thread.dumpStack(). Either that or create a no-args constructor and set a break-point on it in the debugger, just to see who is actually creating the object – jwells131313 Feb 27 '14 at 12:15
  • It's not being called at all, if I place it in a @Service class. – Dejell Feb 27 '14 at 17:02
  • I don't mind using ServiceLocatorUtilities, but where do I add it? what do I send as the first parameter (locator)? – Dejell Feb 27 '14 at 17:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48608/discussion-between-jwells131313-and-dejel) – jwells131313 Feb 27 '14 at 19:07

1 Answers1

2

If you create your own object but still want it injected with hk2 you can use the ServiceLocator.inject method. In your case the descriptor HibernateCategoryDAO must be already in the ServiceLocator (perhaps using ServiceLocatorUtilities.addClass). Be aware that the instances you created yourself will not be managed or tracked by HK2 so they cannot be injected into other objects. Of course, you can add the objects you created yourself using ServiceLocatorUtilities.addOneConstant if you DO want it to be injected into other objects.

Hope that helps.

jwells131313
  • 2,364
  • 1
  • 16
  • 26