我试图为我给出的库创建一个外观。在我的外观中,我使用Guice来构造对象图。对象图中的深层是一个代理对象,它具有getURL/setURL方法。 在我的外观中,如何获取用于创建我的根对象的Proxy实例?我希望我的外观具有URL getter和setter。在Guice对象图中获取对象
我想是这样的:
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个对象笨拙了。
基本上,我需要在创建后在对象图中深入配置一个实例,但是我不确定如何获取该实例。
基本上,我需要在创建后在对象图中深入配置一个实例,但我不确定如何获取该实例。 – kalithlev
我想我暗示你并没有真正掌握它,除非它碰巧是一个单身人士。在注入之前,您必须在注入之前配置对象。在这种情况下,您的模块应为对象http://code.google.com/p/google-guice/wiki/ProvidesMethods或提供商http://code.google.com/p/google-吉斯/维基/ ProviderBindings。可能使用特定于实例的注释http://code.google.com/p/google-guice/wiki/BindingAnnotations。因此,如果您的外观需要新的代理配置,请为此外观定义特定的模块。 – dlamblin