In your module implementation you can do this;
public void bind() {
Foo barTrue = new Foo(true);
bind(Foo.class).annotatedWith(TrueBar.class).toInstance(barTrue);
Foo barFalse = new Foo(false);
bind(Foo.class).toInstance(barFalse);
}
If an annotation is present, Guice will inject barTrue, If not, then it will
inject barFalse;
On Thu, Jun 3, 2010 at 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]<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.