Dropwizard HK2 injection

我与影子孤独终老i 提交于 2019-12-01 22:29:45

If you are going to instantiate the service yourself then it is not going to go through the DI lifecycle and will never be injected. You can let the container create the service if you just register the services as a class

bind(ContentModuleManager.class)
    .to(ContentModuleManager.class)
    .in(Singleton.class);

On the other hand, if you are creating all the services yourself and you have all of them available, why don't you just not use the DI container at all? Just pass all the services through the constructor. Whether you are using constructor injection1 or manually passing through the constructor, getting the service through the constructor is good practice anyway, as it allows for easier testing of the service.


1 - Constructor injection

private Service service;

@Inject
public OtherService(Service service) {
   this.service = service;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!