On 5 November 2010 10:59, Simone Tripodi <[email protected]> wrote:
> Hi all guys,
> I'm in trouble with a silly question but I hope you don't mind and
> provide me a little help :P
>
> Given the simple POJO created by Guice:
>
> {{{
> @Singleton
> public class MyConfig {
>
> @Inject @Named("myconfig.autoclose") boolean autoClose(); // please
> note the type
>
> // setter here
>
> }
> }}}
>
> immagine the proeprty come from a text file, if in the module I bind
> the property via:
>
> {{{
>
> binder.bindConstant().annotatedWith(Names.named("myconfig.autoclose")).to(myAlchemyObject.getStringValue());
> }}}
>
> or, like in the com.google.inject.name.Names class:
>
> {{{
> binder.bind(Key.get(String.class,
>
> Names.Named("myconfig.autoclose"))).toInstance(myAlchemyObject.getStringValue());
> }}}
>
> things work well, the String will be successfully (if the value
> matches) to boolean and the startup works well.
>
> Problems come when binding the String to a Provider<String>:
>
> {{{
> binder.bind(Key.get(String.class,
> Names.Named("myconfig.autoclose"))).toProvider(new Provider<String>()
> {
> public String get() {
> return myAlchemyObject.getStringValue();
> }
> });
> }}}
>
> the Injector raises an error:
>
> {{{
> 1) No implementation for java.lang.Boolean annotated with
> @com.google.inject.name.Named(value=myconfig.autoclose) was bound.
> while locating java.lang.Boolean annotated with
> @com.google.inject.name.Named(value=myconfig.autoclose)
> for field at MyConfig.autoClose(MyConfig.java:XX)
> }}}
>
> Any idea why?
well looking at the convertConstantStringBinding method, it only considers
String bindings which are constant - ie. those bound using bindConstant or
toInstance - so your Provider binding is never considered for conversion
I think this is a conscious design decision rather than an implementation
detail - iirc this wasn't meant to be a full-featured type conversion system
but something to help with basic configuration (hence constant values)
Any suggestion will be more than appreciated, thanks in advance!
> Simo
>
> http://people.apache.org/~simonetripodi/
> http://www.99soft.org/
>
> --
> 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.
>
>
--
Cheers, Stuart
--
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.