Seems easier to create a TrueFooProvider and FalseFooProvider and bind
those to your annotations.

 
bind(Foo.class).annotatedWith(Names.named("TrueBar").toProvider(TrueFooProvider.class);
 
bind(Foo.class).annotatedWith(Names.named("FalseBar").toProvider(FalseFooProvider.class);

Or, I would refactor to something like, if it's a more dynamic state
set during initialization sequence:

 
bind(FooType.class).annotatedWith(Names.named("FooType").to(Boolean.class);

class Foo {
   @Inject Named("FooType")
   private final Boolean fooType;
   .
   .
   .
}

Wonky little scenario but hope it helps.

On Jun 3, 1:44 am, Mirco <[email protected]> wrote:
> Hi guys,
>
> I'm kind of a newbie so I hope I won't made anyone mad on me if the
> question is too silly :)
>
> The problem is the following:
>
> class Foo {
>   private boolean bar;
>   @Inject
>   public Foo(boolean bar) {...}
>
>   public doIt() {
>     if(bar) { goForIt();}
>     else { abortNow();}
>   }
>
> }
>
> I want guice to create instances of Foo that sometimes have the 'bar'
> member set to true and other times it should be set to false. Ideally,
> I would be using it like this:
>
> class Bar {
>   @Inject
>   public Bar(@TrueBar Foo foo) {
>     //here Foo.bar should be set to true
>   }
>
> }
>
> class Baz {
>   @Inject
>   public Bar(Foo foo) {
>     //here Foo.bar should be set to false
>   }
>
> }
>
> How can I achieve this? (if the annotation is not necessary or there
> is a better way to code this I'm open to suggestions ;)
>
> Thanks a lot.
>
> Cheers,
>   Mirco

-- 
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.

Reply via email to