im pretty new in working with dropwizard. Currently I'm trying to implement the HK2 dependency injection. That works pretty fine inside a resource but it doesn't work outside of a resource. Here is what I'm doing:
Client client = new JerseyClientBuilder(environment).using(configuration.getJerseyClientConfiguration()).build("contentmoduleservice"); //DAOs ContentModuleDAO contentModuleDAO = new ContentModuleDAO(hibernate.getSessionFactory()); ModuleServedDAO moduleServedDAO = new ModuleServedDAO(hibernate.getSessionFactory()); //Manager ContentModuleManager moduleManager = new ContentModuleManager(); EntityTagManager eTagManager = new EntityTagManager(); ProposalManager proposalManager = new ProposalManager(client, configuration); environment.jersey().register(new AbstractBinder() { @Override protected void configure() { bind(eTagManager).to(EntityTagManager.class); bind(contentModuleDAO).to(ContentModuleDAO.class); bind(moduleServedDAO).to(ModuleServedDAO.class); bind(proposalManager).to(ProposalManager.class); bind(moduleManager).to(ContentModuleManager.class); } }); I create Instance of the classes I want to be injectible and bind them.
Inside my resource the injection works:
@Api @Path("/api/contentmodule") public class ContentModuleResource { static final Logger LOG = LoggerFactory.getLogger(ContentModuleResource.class); static final int MAX_PROPOSALS_PER_MODULE = 10; @Inject private ContentModuleDAO contentModuleDAO; @Inject private EntityTagManager eTagManager; @Inject private ProposalManager proposalManager; @Inject private ContentModuleManager contentModuleManager; All these variables are filled with an Instance of the right class.
The problem is: The ContentModuleManager should also get some of these classes via injection:
public class ContentModuleManager { @Inject private ContentModuleDAO contentModuleDAO; @Inject private ProposalManager proposalManager; @Inject private ModuleServedDAO moduleServedDAO; But those are null. Can somebody explain a dropwizard noob why this happens and how I can fix this? :D
Thanks!