KAR*_*ván 5 java provider dependency-injection guice
根据Guice 的ThrowingProvider文档,我有以下界面:
public interface IConfigurableProvider<T> extends ThrowingProvider<T, ConfigException> {}
Run Code Online (Sandbox Code Playgroud)
我有多个实现此接口的类,假设我有以下内容:
public class SomethingProvider extends ConfiguredProvider implements IConfigurableProvider<Something> {}
Run Code Online (Sandbox Code Playgroud)
当然这个类实现了必要的方法:
public Something get() throws ConfigException { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
在我的模块中,我有以下代码 MyModule.java
ThrowingProviderBinder.create(binder())
.bind(IConfigurableProvider.class, Something.class)
.to(SomethingProvider.class);
Run Code Online (Sandbox Code Playgroud)
但是,当我启动我的应用程序时,产生以下错误:
6) No implementation for com.package.Something was bound.
while locating com.package.Something
for parameter 5 at com.package.OtherClass.<init>(OtherClass.java:78)
at com.package.MyModule.configure(MyModule.java:106)
Run Code Online (Sandbox Code Playgroud)
我真的不知道我应该从哪里开始寻找这个bug.
更新:即使我也设置了范围,它也会提供相同的错误:
ThrowingProviderBinder.create(binder())
.bind(IConfigurableProvider.class, Something.class)
.to(SomethingProvider.class)
.in(Singleton.class);
Run Code Online (Sandbox Code Playgroud)
Something您需要注入以下内容,而不是注入IConfigurableProvider<Something>:
public class OtherClass {
@Inject
public OtherClass(IConfigurableProvider<Something> somethingProvider) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
这是因为只有在OtherClass的代码(或使用 实例的任何代码Something)中,您才能处理 Provider 抛出的异常,例如ConfigException您的情况。