FYI, I submitted an improvement <https://github.com/google/guice/commit/842f351c4b2b3b0a90d9f3bcf164d8fd19aede6c> to this the other day so that when frameworks call *OptionalBinder.newOptionalBinder(binder(), Foo.class)*, then users can supply the binding by just calling *bind(Foo.class).to(FooImpl.class)*. Frameworks can inject *Optional<Foo*>, which will be absent if users didn't Foo to anything, and present with the user's bound value if they did bind it.
This should allow people to easily swap out *@com.google.inject.Inject(optional=true) Foo* with *@javax.inject.Inject Optional<Foo>*, and also make it easier to use optional injection in constructors. sam On Tue, Apr 1, 2014 at 8:33 PM, Sam Berlin <sber...@gmail.com> wrote: > Ever wanted to: > 1) Set default binding in a library or framework that users can change? > 2) Inject something into a library or framework that isn't required, but > would be better if a user could set it? > 3) Inject an optional value into a constructor? > > OptionalBinder lets you do that. Christian just pushed out some of our > recent internal changes, including the introduction of OptionalBinder to > the multibindings extension. > > Some example usage: > > 1) Setting a default binding that a user can override: > Library: *OptionalBinder.newOptionalBinder(binder(), Foo.class)* > * .setDefault().to(DefaultFoo.class);* > At this point, code can inject '*Foo*' (or *Optional<Foo>)*, and will > get a *DefaultFoo.* > > User: *OptionalBinder.newOptionalBinder(binder(), Foo.class)* > * .setBinding().to(CustomFoo.class);* > Once a user calls setBinding(), injections of *Foo* (or > *Optional<Foo>)* will start providing *CustomFoo* instead. > > 2) Setting a binding that isn't required (e.g, no default value), but a > user can supply. > Library: *OptionalBinder.newOptionalBinder(binder(), Foo.class);* > At this point, code *can't* inject *Foo* -- it will fail saying the > binding isn't supplied. Code *can* (and should) inject *Optional<Foo>*, > though, and the optional will be absent. > > User: *OptionalBinder.newOptionalBinder(binder(), Foo.class)* > * .setBinding().to(CustomFoo.class);* > Once a user calls setBinding(), *Optional<Foo>* will be present and > supply the *CustomFoo*. Also, *Foo* can be directly injected now > (although only user could should do that, since the user code is the only > part that guarantees the binding will be there -- library code should still > inject the* Optional<Foo>*). > > Please report back if you have any issues or suggestions for improvement. > > Thanks! > > sam > -- You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+unsubscr...@googlegroups.com. To post to this group, send email to google-guice@googlegroups.com. Visit this group at http://groups.google.com/group/google-guice. To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/CAJEBNUfFrqhJghyHJpmHX-jqYo4hsEFLuunSZPMRc1kEajQEXA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.