Books I Like

Wednesday, October 21, 2009

Google Guice : Part 2 : Inject Config Value

Was Spring 3.0 just released ?

Right, Here is a link mentioning the same Spring3.0 release. I am really excited to check it out, specially the REST part of it. But yeah, let us save it for other posts.

I directly came here, where is the part One ?

If you haven't gone through the part 1 of this article, and are totally new to Google Guice, please check it out and set up your environment as described there.

What is the goal for this article ?

Since in the previous article we were able to set up a working project/program. Let us extend it further. Almost all of the applications needs to have some configuration properties, today we will learn to inject the property value from a config file.

Ummm.. before you dive into it, I just forgot what was the spring bean I used to configure it in a Spring App?

I guess you are probably talking about By the way, the other day I blogged about extending it.

Well...Lets code it :

public interface IPrinter {
public void print();

public class MyDummyPrinter implements IPrinter {
public void print() {
System.out.println("Article Printed !");

public interface IMailSender {
public void sendMail();

public class MyDummyMailSender implements IMailSender {
public void sendMail() {
System.out.println("Mail Sent !");

public interface ArticleService {
public void publishArticle();

public class Article implements ArticleService {
private IMailSender mailSender;
private IPrinter printer;

private String advertizedAt;
private String articleHeading;

@Named("") String advertizedAt,
@Named("") String articleHeading,
IMailSender mailSender, IPrinter printer) {
this.mailSender = mailSender;
this.printer = printer;
this.advertizedAt = advertizedAt;
this.articleHeading = articleHeading;
System.out.println("This article has been advertized at :" + advertizedAt +
" with the heading :" + articleHeading);

public void publishArticle() {

Check out the code. We want to inject two properties namely "advertizedAt" and "articleHeading" throgh a config file. As you can see Guice comes with a built in annotation - named. So, as it appears, Guice should inject the value for these keys as defined in the config file.

Well, let us define our property file :

But How will Guice know the property value unless it loads it. Does it has something like PropertyPlaceholderConfigurer ?

You are right, we are going to write some code to load the property file. As of now, I am not aware about any helper class who does it for us. If you know one please comment, or else I would update it once I know.

import java.util.Properties;

public class MyArticleModule extends AbstractModule {
protected void configure() {
try {
Properties props = loadProperties();
} catch (Exception e) {

private static Properties loadProperties() throws Exception {
Properties properties = new Properties();
ClassLoader loader = MyArticleModule.class.getClassLoader();
URL url = loader.getResource("");
return properties;

So, before injection we are loading the The code is straight forward. The piece important here to note is the use of which creates a constant binding to @Named(key) for each property.

Well , Let us see it in action :


public class MyBootStrapInvoker {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new MyArticleModule());
Article article = injector.getInstance(Article.class);

Sample Run :-

This article has been advertized at :dZone with the heading :googleGuicePartTwo
Mail Sent !
Article Printed !

Is this the best approach to do this way :

I am sure there must be a better way, as Guice always discourages bindings that uses a string, because the compiler can not check the string. So that means, if there is some spelling mistakes, we would see run time error (like we see in spring, if you make spelling mistakes). Let me know, if you know a better way.
Would appreciate your comments.


Anonymous said...

atoxyl lessor luciano unionized managerial flush

Anonymous said...

Nice post and this post helped me alot in my college assignement. Thanks you on your information.

Anonymous said...

dating mature women [url=]gay teen dating[/url] nigeria dating scams usa singles

Get at Amazon