Introduce Guice with Example

The motivation of Guice

  • Guice alleviates the need for factories and the user of new operator in Java
  • Make youself confident with Java's type safe nature.
  • Helps you design better API
  • Easy tests
  • Less boilerplate code
  • Works on Java SE, Java EE, Android and GWT

Let me give you a little taste of Guice by an example.

public interface Shorter {
    String shortenUrl(String text);
}

 

public class UrlShortener implements Shorter {

    public String shortenUrl(String text) {
        // Local or remote URL shorted service request here
        return "short-url";
    }
}

 

public interface WeiboSender {

    void send(String text);
}

 

public class WeiboBrowserSender implements WeiboSender {

    @Override
    public void send(String text) {
        // Send weibo.
        System.out.println("Weibo was sent out: " + text);
    }
}

 

import com.google.inject.Inject;

/**
 * Weibo client for posting, first it will try to shorten url, then send the weibo out.
 * <p/>
 * User: George Sun
 * Date: 7/12/13
 * Time: 7:25 PM
 */
public class WeiboClient {
    private WeiboSender sender;
    private UrlShortener urlShortener;

    // Note the Inject here, this is the key of this example.
    @Inject
    public WeiboClient(WeiboSender sender, UrlShortener urlShortener) {
        this.sender = sender;
        this.urlShortener = urlShortener;
    }

    public void postWeibo(String text) {
        String weiboText = getWeiboText();
        if (weiboText.length() > 140) {
            weiboText = urlShortener.shortenUrl(weiboText);
        }

        if (weiboText.length() <= 140) {
            sender.send(weiboText);
        }
    }

    private String getWeiboText() {
        return "Sample Weibo text here.";
    }
}

 

import com.google.inject.AbstractModule;

/**
 * Tell Guice what to inject, then Guice will inject instances when needed.
 * <p/>
 * User: George Sun
 * Date: 7/12/13
 * Time: 7:40 PM
 */
public class WeiboModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(WeiboSender.class).to(WeiboBrowserSender.class);
        bind(Shorter.class).to(UrlShortener.class);
    }
}

 

import com.google.inject.Guice;
import com.google.inject.Injector;

/**
 * Driver class.
 * <p/>
 * User: George Sun
 * Date: 7/12/13
 * Time: 7:44 PM
 */
public class Main {

    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new WeiboModule());
        WeiboClient weiboClient = injector.getInstance(WeiboClient.class);

        String sample = "Sample weibo text here.";
        weiboClient.postWeibo(sample);
    }
}

 

For complete reference and documents, visit Guice project home.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值