2010
08.10

I never remember the syntax of Null Providers in Google Guice, posting it here for future reference.

Given a Foo feature (i.e. a Foo interface, implemented by FooImpl), the Guice config may look like:

if (isFooEnabled) {
    binder.bind(Foo.class).to(FooImpl.class);
    log.info("Foo feature enabled");
} else {
    binder.bind(Foo.class).toProvider(Providers.<Foo>of(null));
}

Don’t forget to make Foo @Nullable:

@Inject
public Something(
    @Nullable Foo foo
)
{
   this.foo = foo;
}

See a real-life example in Goodwill on github.

No Comment.

Add Your Comment