gpt4 book ai didi

java - Guice @为单例提供两次依赖实例

转载 作者:行者123 更新时间:2023-11-30 04:16:25 24 4
gpt4 key购买 nike

我想知道如何将单例绑定(bind)到提供程序中的参数。

即:

@Singleton
public class RunButtonController{
@Inject
public RunButtonController(EventBus eventbus){ ... }

public JComponent getView(){
return view;
}
...
}

public class UncaughtExceptionController{
@Inject
public UncaughtExceptionController(
EventBus eventBus, ...,
@Named(DefaultExceptionViewKey) JComponent defaultView)
{ ... }
...
}

public class GUIController{
//another consumer of RunButtonController, arguably the "main" consumer.
@inject
public GUIController(RunButtonController runButtonController,
UncaughtExceptionController uncaughtExceptionController, ...)
{ ... }
...
}

public class Bootstrapper{
public static void main(String[] args){

Injector injector = Guice.createInjector(new OptipModule());

GUIController controller = injector.getInstance(GUIController.class);
}

private static class OptipModule extends AbstractModule{

@Override
protected void configure() {
bind(EventBus.class).toInstance(new EventBus("OPTIPGlobalEventBus"));
bind(JFrame.class).to(GUIView.class);
}

@Provides @Named(DefaultExceptionViewKey)
public JComponent getFrom(RunButtonController runButtonController){
return runButtonController.getView();
}
}
}

在我的 RunButtonController 构造函数上放置一个断点,我可以看到它始终被实例化两次。我希望它只被实例化一次,并且我想要defaultExceptionViewProvider == runButtonControllertrue

我相当广泛地使用了 CaSTLe Windsor,但这是我使用过的唯一 IOC 容器,所以我对 guice 还很陌生。我在各处都看到了访问者行为的残余,并且 guice 的文档非常清楚地表明,类的定义行为(即,实例一次,使用此实例,使用此工厂,...)不会持续存在超过它所配置的模块。我想说的是,我看到它写道,当您使用 @Provides 时,guice 会为您创建一个子模块,所以大概我需要做的就是告诉这个子 @Provides 生成的模块'嘿,这个类是一个单例,我正在解决它,所以就在这里!不要使用你自己的!'

我认为我对这个框架的理解方式是错误的。我一直在捣毁注释并进行调试,但也许我真正需要做的是花几个小时阅读一篇好的教程,不幸的是我找不到一个。 JavaDoc 有示例,网页也发布了它们,但它们只提供了很少的上下文,因此,在读了 @Assisted 上的文章三遍后,我仍然不明白它(也许这就是我的意思)应该使用?)对于那些指向特别详细的博主和他的页面上的 guice 条目的人来说,可以获得奖励积分。

沿着这些思路,非常离题,我想知道我尝试将“嘿,你的默认通知区域是其他人的 View ”插入我的 IOC 容器会产生什么后果。这可能是领域逻辑吗?我真的不希望 UnhandledExceptionController 知道它的 View 是由 RunButtonController 提供的,同样,我也不希望 RunButtonController知道它的 View 除了被标记到 View 树上之外还有其他用途。

感谢您的阅读!

最佳答案

正如发布的那样,看起来您的代码应该可以工作。也就是说,有一些警告可能会导致单例共存。仔细检查每个构造函数调用的堆栈跟踪。

  • 这可能是显而易见的,但您可以在 Guice 的控制之外创建任意数量的实例,而 Guice 无法知道这些实例的存在。仔细检查代码中是否没有任何内容手动调用 RunButtonController 构造函数。

  • 单例行为在任何给定的注入(inject)器中强制执行。如果您的应用程序中有两个或多个注入(inject)器,它们每个都可以创建自己的 RunButtonController 实例。不过,在父注入(inject)器中声明的单例对于任何子注入(inject)器都是可见的。

  • 单例通过 key 工作。如果您要删除 @Singleton 注释并添加以下内容:

    bind(RunButtonController.class)
    .annotatedWith(Names.named("Foo"))
    .asEagerSingleton()

    然后注入(inject) RunButtonController 每次都会给你一个新的实例,但是注入(inject) @Named("Foo") RunButtonController 会给你一个单独的单例实例,该实例将返回每次都一样。这可能不适用于您,因为 @Singleton 位于类本身上,但它之前已经咬过其他人了。

  • 您似乎并不依赖继承,但请记住 singleton annotations don't inherit from superclass to subclass .

旁注:@Provides 方法不能通过子注入(inject)器工作,但私有(private)模块可以(称为“父注入(inject)器”in the documentation)。在内部,确实是 separate internal module负责调用这些提供者方法,但这并不重要——单例在模块之间共享。

关于你关于共享 View 的题外话:通过注入(inject) @Named(DefaultExceptionViewKey) JComponent 而不是 RunButtonController,你已经做得相当不错了,但是如果你想保持平衡的话与实现无关,您可以创建一个 ExceptionHandlerComponent 接口(interface)并针对该接口(interface)编写代码。

关于java - Guice @为单例提供两次依赖实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18239654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com