GIN 是否支持子注入器之类的东西?

发布于 2024-12-28 19:52:59 字数 342 浏览 5 评论 0原文

我有一个包含子应用程序的应用程序。我想隔离 GIN 注入,以便每个子应用程序都可以拥有相同核心共享类的单独实例。我还希望注入器将一些核心模块中的类提供给所有子应用程序,以便可以共享单例实例。例如,

GIN Modules:
  Core - shared
  MetadataCache - one per sub-application
  UserProvider - one per sub-application

在 Guice 中,我可以使用 createChildInjector 来完成此操作,但我在 GIN 中看不到明显的等效项。

我可以在 GIN 中实现类似的功能吗?

I have one application that contains sub-applications. I would like to segregate the GIN injection so that each sub-application can have separate instances of the same core shared classes. I also want the injector to supply classes from some core modules to all sub-applications, so that singleton instances can be shared. e.g.

GIN Modules:
  Core - shared
  MetadataCache - one per sub-application
  UserProvider - one per sub-application

In Guice I can do this using createChildInjector, but I can't see an obvious equivalent in GIN.

Can I achieve something similar in GIN?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

夜访吸血鬼 2025-01-04 19:52:59

感谢 @Abderrazakk 提供的链接,我解决了这个问题,但由于该链接不是很容易找到说明,我想我也应该在这里添加一个示例解决方案:

私有 GIN 模块允许您进行单级分层注入,其中在私有模块内注册的类型仅对该模块内创建的其他实例可见。在任何非私有模块中注册的类型仍然可供所有人使用。

示例

让我们有一些要注入(和注入)的示例类型:

public class Thing {
    private static int thingCount = 0;
    private int index;

    public Thing() {
        index = thingCount++;
    }

    public int getIndex() {
        return index;
    }
}

public class SharedThing extends Thing {
}

public class ThingOwner1 {
    private Thing thing;
    private SharedThing shared;

    @Inject
    public ThingOwner1(Thing thing, SharedThing shared) {
        this.thing = thing;
        this.shared = shared;
    }

    @Override
    public String toString() {
        return "" + this.thing.getIndex() + ":" + this.shared.getIndex();
    }
}

public class ThingOwner2 extends ThingOwner1 {
    @Inject
    public ThingOwner2(Thing thing, SharedThing shared) {
        super(thing, shared);
    }
}

创建两个像这样的私有模块(使用 ThingOwner2 作为第二个模块):

public class MyPrivateModule1 extends PrivateGinModule {
  @Override
  protected void configure() {
    bind(Thing.class).in(Singleton.class);
    bind(ThingOwner1.class).in(Singleton.class);
  }
}

和一个共享模块:

public class MySharedModule extends AbstractGinModule {
    @Override
    protected void configure() {
        bind(SharedThing.class).in(Singleton.class);
    }
}

现在在我们的注入器中注册这两个模块:

@GinModules({MyPrivateModule1.class, MyPrivateModule2.class, MySharedModule.class})
public interface MyGinjector extends Ginjector {
    ThingOwner1 getOwner1();
    ThingOwner2 getOwner2();
}

最后我们可以看到 ThingOwner1 和 ThingOwner2 实例在共享模块中具有相同的 SharedThing 实例,但在其私有注册中具有不同的 Thing 实例:

System.out.println(injector.getOwner1().toString());
System.out.println(injector.getOwner2().toString());

I solved this thanks to the link given by @Abderrazakk, but as the link isn't very forthcoming with instructions I thought I'd add a sample solution here too:

Private GIN modules allow you to have a single level of hierarchical injection, where types registered inside a private module are only visible to other instances created within that module. Types registered inside any non-private modules are still available to all.

Example

Let's have some sample types to inject (and inject into):

public class Thing {
    private static int thingCount = 0;
    private int index;

    public Thing() {
        index = thingCount++;
    }

    public int getIndex() {
        return index;
    }
}

public class SharedThing extends Thing {
}

public class ThingOwner1 {
    private Thing thing;
    private SharedThing shared;

    @Inject
    public ThingOwner1(Thing thing, SharedThing shared) {
        this.thing = thing;
        this.shared = shared;
    }

    @Override
    public String toString() {
        return "" + this.thing.getIndex() + ":" + this.shared.getIndex();
    }
}

public class ThingOwner2 extends ThingOwner1 {
    @Inject
    public ThingOwner2(Thing thing, SharedThing shared) {
        super(thing, shared);
    }
}

Create two private modules like this (using ThingOwner2 for the second one):

public class MyPrivateModule1 extends PrivateGinModule {
  @Override
  protected void configure() {
    bind(Thing.class).in(Singleton.class);
    bind(ThingOwner1.class).in(Singleton.class);
  }
}

And a shared module:

public class MySharedModule extends AbstractGinModule {
    @Override
    protected void configure() {
        bind(SharedThing.class).in(Singleton.class);
    }
}

Now register the two modules in our injector:

@GinModules({MyPrivateModule1.class, MyPrivateModule2.class, MySharedModule.class})
public interface MyGinjector extends Ginjector {
    ThingOwner1 getOwner1();
    ThingOwner2 getOwner2();
}

Finally we can look and see that both ThingOwner1 and ThingOwner2 instances have the same SharedThing instance from the shared module, but different Thing instances from their private registrations:

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