开发者

Guice ThrowingProvider problem

According to the ThrowingProvider documentation of Guice I have the following interface:

public interface IConfigurableProvider<T> extends ThrowingProvider<T, ConfigException> {}

I have multiple classes that implements this interface, let assume I have the following:

public class SomethingProvider extends ConfiguredProvider implements IConfigurableProvider<Something> {}

Of course this class implements the necessary method:

public Something get() throws ConfigException { /* ... */ }

In my module, I have the following code in MyModule.java

ThrowingProviderBinder.create(binder())
    .bind(IConfigurableProvider.class, Something.class)
    .to(SomethingProvider.class);

But when I start my application the following error produced:

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)

I don't really know wher开发者_如何学Goe should I start looking for the bug.

Update: It provides the same error even if I set the scope as well:

ThrowingProviderBinder.create(binder())
    .bind(IConfigurableProvider.class, Something.class)
    .to(SomethingProvider.class)
    .in(Singleton.class);


Rather than injecting the Something, you need to inject the IConfigurableProvider<Something>:

public class OtherClass {
  @Inject
  public OtherClass(IConfigurableProvider<Something> somethingProvider) {
    ...
  }
}

This is because it is only in OtherClass's code (or any code that uses an instance of Something) you can deal with exceptions thrown by the Provider, such as ConfigException in your case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜