Injecting primitives and/or bind toInstance

895 views
Skip to first unread message

astubbs

unread,
Jul 25, 2008, 9:20:59 AM7/25/08
to google-guice
I created a blog post about injecting primitives and/or bind
toInstance because I didn't pick it up straight away off the user
guide and had to google it out a bit.for a clearer example. Just
wanted to share the love! Couldn't find anyone asking questions about
it in the forum either, so must be just me!
http://stubbisms.wordpress.com/2008/07/26/google-guice-injecting-primitives-in-constructors/

Main problem was that I was reading the user guide too fast I think!

Dhanji R. Prasanna

unread,
Jul 25, 2008, 7:52:21 PM7/25/08
to google...@googlegroups.com
Interesting.

You don't have to use the named annotation, you can use any binding annotation. Plus one minor advantage strings in @Named have over bean-ids in Spring is that you can point to a string constant checked at compile time:

bindConstant().annotatedWith(named(AN_INT)).to(12);

//elsewhere..
@Inject @Named(AN_INT) int x;

Dhanji.

astubbs

unread,
Jul 25, 2008, 8:50:40 PM7/25/08
to google-guice
Ah yes I think I gathered that but I was hell bent on not having to
actually create my own annotation just to inject a string into my
constructor args. Is that actually what you are refering to?

So AN_INT - is that an annotation you'd have to create? It would
really help my understanding if you could complete the example you
posted?

On Jul 26, 11:52 am, "Dhanji R. Prasanna" <dha...@gmail.com> wrote:
> Interesting.
>
> You don't have to use the named annotation, you can use any binding
> annotation. Plus one minor advantage strings in @Named have over bean-ids in
> Spring is that you can point to a string constant checked at compile time:
> bindConstant().annotatedWith(named(AN_INT)).to(12);
>
> //elsewhere..
> @Inject @Named(AN_INT) int x;
>
> Dhanji.
>
> On Fri, Jul 25, 2008 at 11:20 PM, astubbs <antony.stu...@gmail.com> wrote:
>
> > I created a blog post about injecting primitives and/or bind
> > toInstance because I didn't pick it up straight away off the user
> > guide and had to google it out a bit.for a clearer example. Just
> > wanted to share the love! Couldn't find anyone asking questions about
> > it in the forum either, so must be just me!
>
> >http://stubbisms.wordpress.com/2008/07/26/google-guice-injecting-prim...

Dhanji R. Prasanna

unread,
Jul 26, 2008, 3:06:21 AM7/26/08
to google...@googlegroups.com
On Sat, Jul 26, 2008 at 10:50 AM, astubbs <antony...@gmail.com> wrote:
>
> Ah yes I think I gathered that but I was hell bent on not having to
> actually create my own annotation just to inject a string into my
> constructor args. Is that actually what you are refering to?
>
> So AN_INT - is that an annotation you'd have to create? It would
> really help my understanding if you could complete the example you
> posted?

//module class:
public static final String AN_INT = "anInt";

...configure() {
bindConstant().annotatedWith(named(AN_INT)).to(12);
}


//elsewhere:
@Inject @Named(MyModule.AN_INT) int x;

Dhanji.

astubbs

unread,
Jul 26, 2008, 3:22:11 AM7/26/08
to google-guice
Ah yes of course, I see what you were saying now...

In other news...

I've updated my blog with some examples that I was inspired from
reading a post on Dhanji R. Prasanna's blog:
http://stubbisms.wordpress.com/2008/07/26/google-guice-injecting-primitives-in-constructors/

My now favourite module binding for my use case is:
<pre>
String DEFAULT_PET_NAME = "suki";

bind(HouseHoldPet.class).toProvider(new Provider<HouseHoldPet>()
{
public HouseHoldPet get()
{
return new HouseHoldPet(DEFAULT_PET_NAME);
}
});
</pre>

On Jul 26, 7:06 pm, "Dhanji R. Prasanna" <dha...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages