Guice @Nullable annotation

2019-02-23 16:41发布

问题:

In my service, I have a protected constructor with @Inject and one of the parameters (provider) @Nullable. Any ideas why I am getting

com.google.inject.CreationException: Guice creation errors:
1) No implementation for [[service]] was bound.

? Guice is 3.0pre1, @Nullable is ours.

回答1:

@Nullable isn't the same as @Inject(optional=true)... I think if you want to inject null, you need to bind(Service.class).toProvider(Providers.<Service>of(null)) or to otherwise have some kind of provider bound that may return null.