- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道如何将单例绑定(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 == runButtonController
为true
。
我相当广泛地使用了 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/
我有很多 TabularResultSet 类的带注释的命名实例,我想将它们传递给多个类并传递给静态最终属性。我该如何注入(inject)?我认为它不喜欢该属性的最终和静态性质。必须在属性声明中直接调
我是 Guice 的新手,我想知道我能走多远。 我有一个接口(interface)UserInfo具有多个实现类 GoogleUserInfo , FacebookUserInfo , Twitter
我的应用程序中绑定(bind)了两个类型的命名实例: bind(Foo.class).toProvider(FooProvider.class); bind(Foo.class).annotatedW
我有一个 Guice 模块,它有一个 @Provides 方法,它接受 2 个参数并返回接口(interface)的实现: public class **ClientModule** extends
请解释在以下使用工厂的场景中使用 Google-Guice 自动连接的正确方法。我正在使用 XML 工厂进行 XSLT 处理。 StringWriter strWriter = new StringW
我有一个 Guice Module提供 List使用 @Provides - 带注释的方法。 class TestModule() : Module { override fun configur
为什么Guice 3.0针对错误的配置组件(例如,缺少@Inject)抛出此异常,而不是格式化消息? Exception in thread "main" com.google.inject.inte
我有以下供应商: public class GuiceResourceProvider implements Provider { @Inject private Configur
使用 Guice-servlet,我们可以轻松地进行 servlet 映射,如下所示: filter(*.jsp).through(MyFilter.class) 但是,谁能告诉我如何将过滤器映射到
我正在尝试向我的项目添加 Swagger 。我们的设置与示例项目略有不同。我们使用 guice 和 guice-servlet 来注入(inject)并启动我们的 JerseyServletModul
假设我有一个 Guice 模块 ProdModule,我想依赖其他 GuiceModule、ProdDbModule 和 ProdPubSubModule。我将如何实现 ProdModule 的 co
我正在考虑在我的应用程序中使用 Guice for DI,我应该能够在运行时交换实现。下面提供了一个示例来说明要求: class ValidationEngine { public void v
我想设置 Guice 绑定(bind),所以我用 Java 创建了一个完美运行的模块: public class CrashLoggerModule extends AbstractModule {
所以我有一个模块将接口(interface)与实现类绑定(bind)。 bind(ILocalStore.class).to(LocalStore.class); 此绑定(bind)是否还会注入(in
guice 4.0 是否向后兼容 3.x?无法从发行说明或常见问题解答中弄清楚... 如果没有,是否有兼容性问题列表? 最佳答案 我没有任何官方来源但根据我的经验,没有任何兼容性问题。 我使用了几个
我正在尝试使用 Guice,并且我来自 Spring。 我想知道 @Inject 是否相当于 Spring 中的 @Autowired 以及我是否可以在 Web 应用程序中使用它,就像在 Spring
我是 Guice DI 的新手。我想弄清楚我的情况。 简单来说,有没有通过Guice @annotations来替代MapBinder的? 我的场景: Interface A{} Class A1 i
我的项目正在使用 Guice作为负责为大型对象图(主要是单例)提供依赖项(服务类)的 IOC 容器。有时,如果在构造过程中依赖项失败,并且许多对象都需要此依赖项,则失败将一遍又一遍地发生,并将异常添加
我有一个类 (CustomConnectionProvider),它将由第三方库 (hibernate) 使用 class.forName().newInstance() 实例化。我需要注入(inje
删除 guice servlet 后,我需要进行一些清理。使用 guice servlet 时是否可以 Hook 到 servlet 破坏?我需要使用喷油器进行清理工作。 我可以覆盖 contex
我是一名优秀的程序员,十分优秀!