获取 Guice 对象图中的对象
我正在尝试为我正在捐赠的图书馆制作一个外观。在我的外观中,我使用 Guice 来构建对象图。对象图的深处是一个 Proxy 对象,它具有 getURL/setURL 方法。 在我的外观中,如何获取用于创建根对象的 Proxy 实例?我希望我的外观有 url getter 和 setter。
我尝试了这样的事情:
public class SomeThingFacade() {
private final SomeThing thing;
private final HTTPProxy proxy;
public SomeThingFacade() {
MyModule module = new MyModule();
Injector injector = Guice.createInjector(module);
// this is the main class I'm making a facade for
this.thing = injector.getInstance(SomeThing.class);
// deep in the "thing" object graph is a Proxy implementation
this.proxy = injector.getInstance(HTTPProxy.class);
}
public void setURL(URL url) {
this.proxy.setURL(url);
}
}
但是injector.getInstance创建了一个新实例。
MyModule 中的绑定:
bind(Proxy.class).to(HTTPProxy.class).asEagerSingleton();
我之前已在外观构造函数中对对象图进行了硬编码,但对于 30 个对象而言,它变得很笨拙。
基本上,我需要在创建后在对象图中深处配置一个实例,但我不确定如何获取该实例。
I'm trying to make a facade for a library I'm giving out. In my facade I use Guice to contruct the object graph. Deep in the object graph is a Proxy object, that has getURL/setURL methods.
In my facade, how do I get the Proxy instance used to create my root object? I want my facade to have url getter and setter.
I tried something like this:
public class SomeThingFacade() {
private final SomeThing thing;
private final HTTPProxy proxy;
public SomeThingFacade() {
MyModule module = new MyModule();
Injector injector = Guice.createInjector(module);
// this is the main class I'm making a facade for
this.thing = injector.getInstance(SomeThing.class);
// deep in the "thing" object graph is a Proxy implementation
this.proxy = injector.getInstance(HTTPProxy.class);
}
public void setURL(URL url) {
this.proxy.setURL(url);
}
}
but the injector.getInstance created a new instance.
Binding in MyModule:
bind(Proxy.class).to(HTTPProxy.class).asEagerSingleton();
I had previously hardcoded the object graph in the facade constructor, but it got unwieldly with 30 objects.
Basically, I need to configure an instance deep in the object graph after creation, but I'm not sure how I get a hold of that instance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来完全是一个很好的严肃问题。但是,我无法弄清楚到底在问什么。
我的答案是:查看代码并忽略 fascad 对象图讨论(如果我完全误解了您,请告诉我):
如果
thing
的SomeThing
依赖于深层内部代理
,则模块应将其配置为绑定到HTTPProxy
对于事情。第二个getInstance
不会影响第一个。您可以以某种方式做一些使proxy
影响thing
的事情的唯一方法是,如果 HTTPProxy 绑定在(Singleton.class)
中,那么通过调用代理上影响HTTPProxy
的成员和行为的方法,这也可能是您可能在做您正在寻找的事情的thing
深处的同一个实例。我不明白你为什么要这样做。考虑编写一个配置HTTPProxy
的提供程序和/或制作一个专门的模块供外观使用。This totally looks like a good serious question. However, I can't figure out what's being asked exactly.
My answer, looking at the code and ignoring the fascaded object graph talk (so let me know if I misunderstood you completely), is:
If
thing
'sSomeThing
depends somewhere on adeep internal proxy
, the module should configure it to be bound toHTTPProxy
for thing. The secondgetInstance
does not affect the first. The only way you could somehow be doing something that makesproxy
affectthing
is if HTTPProxy were boundin(Singleton.class)
, then by calling methods on proxy that affect the members and behavior ofHTTPProxy
which would also be the same instance deep insidething
you might be doing what you're looking for. I don't see why you'd want to do it this way though. Consider instead writing a provider that configuresHTTPProxy
and/or making a special module just for the facade's use.