获取 Guice 对象图中的对象

发布于 2024-12-06 08:12:55 字数 1012 浏览 0 评论 0原文

我正在尝试为我正在捐赠的图书馆制作一个外观。在我的外观中,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揪着可爱 2024-12-13 08:12:55

这看起来完全是一个很好的严肃问题。但是,我无法弄清楚到底在问什么。

我的答案是:查看代码并忽略 fascad 对象图讨论(如果我完全误解了您,请告诉我):
如果 thingSomeThing 依赖于深层内部代理,则模块应将其配置为绑定到 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's SomeThing depends somewhere on a deep internal proxy, the module should configure it to be bound to HTTPProxy for thing. The second getInstance does not affect the first. The only way you could somehow be doing something that makes proxy affect thing is if HTTPProxy were bound in(Singleton.class), then by calling methods on proxy that affect the members and behavior of HTTPProxy which would also be the same instance deep inside thing 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 configures HTTPProxy and/or making a special module just for the facade's use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文