我已读到,明确要求从DI机制中提出反对者,被视为DI/IoC的一般反对者。 这是因为,在物体进入范围时,该物体本应已注入它所需要的所有附属设施。 在Guice-land,这种反家长将代表:
Service svcImpl = (Service)injector.getInstance(Service.class);
Apparently, the one exception to this anti-pattern is the bootstrapping/dependency resolution phase, where you ask Guice for all the top-level root ("root" in the sense that all other objects flow from it) objects.
Interestingly enough, I can t find any practical, working code examples of this bootstrap process after an exhaustive online search! I would imagine it looks something like this:
public void bootstrap() {
RootObj1 root1 = (RootObj1)injector.getInstance(RootObj1.class);
RootObj2 root2 = (RootObj2)injector.getInstance(RootObj2.class);
this.root1 = root1;
this.root2 = root2;
// Since all objects flow from these two "root" dependencies, bootstrapping
// is complete. root1 and root2 contain all the dependencies the application
// needs from here on out, and the "injector" object is no longer needed.
// More importantly, injector is never used anywhere else in the code, and
// therefore the code is not in violation of this anti-pattern.
}
www.un.org/spanish/ecosoc 这是否正确,或我是否走过这里的地下室? 我只问,因为我找不到任何工作榜样!
As I write this I m beginning to second-guess myself because this just doesn t seem feasible/practical.
这是因为,实际上,有多种不同的“依赖树木”,其根基各不相同。 这将导致需要某种类型的<代码>(Boots Capper的物体,该物体负责归还这些树木的根基,以便整个代码基的所有其他物体都能要求它们所属的“树”有适当的根标。 这已经变得极其复杂。
“working Code example of how poststrapping is practiced in the real world may help make some clear to this for me. 提前感谢!