我有一个 Gin @Inject
我使用 optional=true
的调用属性,但它根本不被尊重/不起作用(并且抛出错误,因为我的类没有绑定(bind),这是真的)。
我通过 .gwt.xml
使用 GIN像这样的文件:
<extend-configuration-property name="gin.ginjector.modules" value="com.example.client.ClientModule" />
在上面的示例中,ClientModule 扩展了 com.google.gwt.inject.client.AbstractGinModule
.
然后我有一个类,我 requestStaticInjection
像这样:
requestStaticInjection(Example.class);
在 Example.java
我有:
class Example {
@com.google.inject.Inject(optional=true)
protected static SomeUnboundInterface someUnboundInterface = null;
...
}
但是我在应用程序启动过程中遇到如下错误:
[INFO] [ERROR] No binding found for com.example.SomeUnboundInterface in com.gwtplatform.mvp.client.DesktopGinjector
[INFO] [ERROR] Errors in 'gen/com/gwtplatform/mvp/client/DesktopGinjectorProvider.java'
[INFO] [ERROR] Line 8: Failed to resolve 'com.gwtplatform.mvp.client.DesktopGinjector' via deferred binding
嗯,这确实是事实;我的 SomeUnboundInterface 确实没有绑定(bind);但是我希望使用 optional=true
属性会起作用。
请注意,官方文档并没有说对于GIN,支持该属性。然而,it does say so for GUICE ,所以我希望它也适用于 GIN。
您是否知道任何解决方案,以便在未绑定(bind)特定依赖项时我不会收到错误?
编辑:忘记提及版本。 GWT 2.8.1; Gin 2.1; GWTP 1.6
最佳答案
可选应该有效,此示例使用 GWT 2.8.1 和 GIN 2.1.2 编译并打印“null”。
static class Foo {
@Inject(optional = true) Runnable bar;
}
static class MyModule extends AbstractGinModule {
@Override protected void configure() { bind(Foo.class); }
}
@GinModules(MyModule.class) interface MyGinjector extends Ginjector {
Foo foo();
}
@Override public void onModuleLoad() {
L.log(GWT.<MyGinjector>create(MyGinjector.class).foo().bar);
}
但是,如果您添加静态注入(inject)(如示例中所示),则会失败。您可以将其绑定(bind)到某些东西或删除“requestStaticInjection”。
关于java - Google GIN、GWT 和 com.google.inject.Inject(可选=true)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46569245/