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.
相关推荐
team introduce.key
【知识点详解】 1. 自我介绍:在面试中,个人的自我介绍是非常关键的第一步。HHJ,26岁,他描述自己是一个勤奋、开放、随和、有责任感的人,学习能力强,适应新环境快,并且喜欢团队合作。他的工作态度严谨负责,这...
Updated for Excel 2016 and based on the bestselling editions from previous versions, Microsoft Excel 2016 Programming by Example with VBA, XML and ASP is a practical, how-to book on Excel programming,...
Introduce to Algorithms, A Creative Approach .英文版
### 数值计算与MATLAB简介 #### 一、引言:MATLAB与数值计算概览 本书旨在介绍两个核心主题:MATLAB与数值计算。MATLAB作为一种强大的数值计算工具,在科学计算、工程分析以及数据分析等领域有着广泛的应用。...
This book aims to introduce readers to the fundamental concepts of general-purpose GPU programming using CUDA. The book's target audience includes programmers, researchers, and students interested in...
### SST_MCU_Introduce选型知识点详解 #### 一、SST89系列单片机概述 SST89系列单片机属于MCS-51标准系列,由深圳科赛电子提供支持和服务。该系列单片机具备高度兼容8052系列单片机的特点,并在此基础上进行了多项...
Linux文件系统是一个多层次的存储数据和文件的结构,它提供了一种组织和存储数据的方式,以便用户可以长期保存、共享和访问这些数据。这个系统是操作系统中最重要的组成部分之一,因为它不仅存储数据,还提供了一套...
Chapter 2, Classifying Handwritten Digits with a Feedforward Network, will introduce a simple, well-known and historical example which has been the starting proof of superiority of deep learning ...
Linux操作系统介绍 Linux,一种基于开源的类Unix操作系统,由芬兰程序员林纳斯·托瓦兹在1991年创建,至今已发展成为一个全球性的、充满活力的生态系统,被广泛应用于服务器、超级计算机、嵌入式设备以及移动设备等...
We do, however, introduce data science and the R language briefly, with many resources for the reader to go learn those disciplines, as well, which puts this book within the reach of database ...
With part 4, “Writing queries,” we introduce the data query features and cover query languages and APIs in detail. Not all chapters in this part are written in a tutorial style; we expect you’ll ...
Chapter 7, Implementing a Spam Filter with Bayesian Learning, will introduce you to probability theory, and show you how you can use Bayesian inference to classify emails as spam or not. Chapter 8, ...
OSGi(Open Services Gateway Initiative)是一种面向Java的动态模块系统,旨在解决软件的复杂性和可扩展性问题。它提供了一种标准化的框架,使得开发者可以创建可重用、可协作的组件,并且能够在运行时动态地安装、...
**Spring 框架简介** Spring 是一个开源的 Java 应用程序开发框架,由 Rod Johnson 在其著作《Expert One-on-One J2EE Design and Development》中首次提出。它为开发者提供了一个配置管理工具,解决了企业应用开发...
In this book, I introduce you to the field of robotics. The journey will be challenging; it’s intended to be. But by the end of the book, you will have hands-on exposure to many of the ...