English 中文(简体)
冰盖:boot和依赖图表解决
原标题:Guice: bootstrapping and dependency graph resolution

我已读到,明确要求从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. 提前感谢!

问题回答

我认为,你重新找不到一个很好的例子的原因是,你重新使其比需要更加复杂。 boot比您的代码刀切更为复杂,但你可能需要在<代码>root1或<代码>上援引一种方法,使你的申请开始做任何事情!

其余的由Guice处理。 在执行过程中出现的任何物体均通过从Guice获得所需物品。 Inject -annotated Constructionor (or methods). 没有必要有一个控制所有树木根源的诱杀装置,因为DI集装箱管理所有情况。 如果一个树的物体在另一个树中需要一个物体,请上@ Inject 无需“ask”诱杀装置。 有时很难想象有<条码>服务/密码>背景的人,但设计班级确实是一个更加一致的方法。

冰有几种管理依赖图表的工具。 不同的依赖树木通过<modules管理,这些树木只是一组具有凝聚力的树木。 因此,古冰鼓励你cap起功能,让注射者解决相互依赖的问题。 此外,Guice在需要之前(除能够急切装的单一州外)立即发送一张图表,开发商可使用<代码>Provider接口和方法明确推迟即时发送。

或许,如果你解释你想要利用古冰的用途,或解释你再次使用哪些用途,那将有所帮助。 这将给我们一个更好的想法,了解什么时候才有意义。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签