You can achieve what you want using a technique described on the FAQ page: http://code.google.com/p/google-guice/wiki/FrequentlyAskedQuestions#How_can_I_inject_optional_parameters_into_a_constructor ?
It's not the most elegant thing in the world (you'll need a *Holder class for each optional parameter, and you'll have to change the type of your ctor args), but it gets the job done and does so without requiring any nasty setter/field injection. Chris On Fri, Jul 2, 2010 at 2:37 AM, Maaartin-1 <[email protected]> wrote: > IMHO, it's a sort-of bug or a missing feature. There's no nice solution > for the problem described. Quite often, a class knows suitable defaults > for missing ctor parameters and something like this should be supported. > Probably @Nullable is fine the way it works, but then there should be an > @Optional annotation on arguments allowing both a binding to null and a > missing binding. > > You can achieve something like > > @Inject > public void injectWebServiceProperties( > @Optional @Named("akui.webServiceURL") String webServiceUrl, > @Optional @Named("akui.webServiceUsername") String webServiceUsername, > @Optional @Named("akui.webServicePassword") String webServicePassword) { > ... > } > > only using field injection, which is obviously a bad thing. I've already > ran into this several times and I don't think it's so rare it should be > ignored. > > On 10-07-02 10:31, [email protected] wrote: > > On Jul 1, 3:30 pm, Jeremy Chone <[email protected]> wrote: > >> Is this a wanted feature or a bug? Is there any way to avoid to have > >> one setter per property while keeping any of them optional? > > > > It's a feature. Although they're frequently confused, 'optional=true' > > and @Nullable mean quite different things. > > > > '@Inject(optional=true)' means that the injector should suppress any > > error from not being able to fulfill the dependencies of an injection. > > The injector will try its best to find bindings, even if that involves > > creating just-in-time bindings. But if any of the parameters to a > > method cannot be satisfied, the entire call is skipped. > > > > '@Nullable' means that the injector will not report an error if a null > > value is returned by the binding for an injection. The binding must > > still exist. The only time @Nullable is useful is when you implement a > > provider method (@Provides) or a provider class that may return null. > > -- > You received this message because you are subscribed to the Google Groups > "google-guice" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<google-guice%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en. > > -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
