Stubbisms – Tony’s Weblog

July 26, 2008

Google Guice – Injecting primitives in constructors

Filed under: Java — Tags: , , , , , , — Antony Stubbs @ 2:10 am

Guice‘s user guide could definitely do with some work. It’s ok in some parts but overall it leaves a lot “up to the imagination” if you know what I mean.

I’d been googling various combinations of the words of the title of this blog post, and the Guice forums but couldn’t find an example or explanation of what I was looking for. I really think the User Guide needs this example in there.

For one thing, they definitely need more usage examples.

For example, they do not clearly show how to inject primitives into an injected constructor! I couldn’t figure it out from reading the user guide, but finally after some very random Google link clicking, I found an example. This post is to help try and bring this example to the fore front for other people to see.

To do this, I have now discovered nearly by fluke:

bindConstant().annotatedWith(Names.named( "dogs  name" )).to("suki");

and then in your constructor for the Guice managed object where the String is to be injected:

@Inject
public HouseHoldPet ( @Named( "dogs name" ) String myDogsName);

Pretty easy really now that I see it. Didn’t really understand the information around the @Named annotation in the user guide, but after seeing this example, I can see how it works.

Of course as far as Guice vs Spring is concerned – you’re *still* left with the task of keeping the key names in sync.

Oh First Class Properties, how I miss thee!

</adam sandler>3 weeks later…</adam sandler>

De’ night time is de’ right time… De’ night time is de’ right time…

Of course, I could have read this from the user guide, a little closer:

// Bind Bar to an instance of Bar.
Bar bar = new Bar();
binder.bind(Bar.class).toInstance(bar);

But there I suppose I was reading too fast, and it wasn’t sooo obvious. Honest.

This of course means that the above, as long as your requirements are pretty basic as mine were, can be replaced with:

bind(Animals.class).toInstance(new HouseHoldPet("suki");

Which would allow me to still have the pet name managed by the module configuration but allow the class to be automatically injected into dependent classes.

The draw back to using this approach is that you’re basically using a singleton. You probably don’t want to do that.

Along with the user guide, the Java Doc for the Binder Interface which I have also just discovered, actually has some other good examples with good explanations. However, the user guide again does not direct you here.

And yes, the 3 weeks later is an Adam Sandler joke 🙂 – it was more like, ‘later that night’… 😉

Update…

I have come across a new blog by Dhanji R. Prasanna which I found in response to my post about this blog on the Guice forum.

One of his blog posts on his thoughts on closures for java, contained a very succinct example of the Provider Interface:

bind(Service.class).toProvider(new Provider<Service>() {
    public Service get() {
        return new ServiceImpl();
    }
});

Using this technique, I can avoid the hassle of building an annotation for my parameter injection:

bind(Animals.class).toProvider(new Provider<Animals>() {
    public Animals get() {
        return new HouseHoldPet( "suki" );
    }
});

Neato!

And if you have a class which doesn’t implement an Interface, as do I, you can still an in-line factory for said class which injects the primitive:

bind(HouseHoldPet.class).toProvider(new Provider<HouseHoldPet>() {
    public HouseHoldPet get() {
        return new HouseHoldPet( "suki" );
    }
});

Choice!

“suki” of course should be extracted to a local constant – something like DEFAULT_PET_NAME or some such.

4 Comments »

  1. […] bookmarks tagged the primitives Google Guice – Injecting primitives in constructor… saved by 5 others     ignaciomarley bookmarked on 07/30/08 | […]

    Pingback by Pages tagged "the primitives" — July 31, 2008 @ 6:33 am

  2. […] public links >> guice Google Guice – Injecting primitives in constructors Saved by austinmoody on Sun 14-12-2008 The Guice-Barbour two-step Saved by roboperson on Sat […]

    Pingback by Recent Links Tagged With "guice" - JabberTags — December 29, 2008 @ 5:04 am

  3. nice post

    Comment by bakbak baba — April 11, 2014 @ 4:24 am

  4. very clearly explained…

    Comment by bakbak baba — April 11, 2014 @ 4:28 am


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.