`
louisling
  • 浏览: 143113 次
  • 性别: Icon_minigender_1
  • 来自: ZhuHai
社区版块
存档分类
最新评论

Guice

    博客分类:
  • Java
阅读更多
Guice

(from Google Bob Lee's IoC)
Dependency Injection, DI
Inversion of Control, IoC

It's another IoC container, which like a cousin of Spring IoC.
  • Spring uses xml, provide IoC, Aop and peripheral service;[*]Guice uses Annotatin, each has their strongpoint, it's hard to say which one is better
Just use them in proper place !

Usage 1:

//1) 
public interface MyService {
    void myMethod();
}

//2)
public class MyServiceImpl implements MyService {
    public void myMethod() {
        System.out.println("Hello, World!");
    }
}

//3) Add annotation of "@Inject"
public class Client {
    //If define @Inject for this field, the set method isn't required
    MyService service;

    @Inject
    void setService(MyService service) {
        this.service = service;
    }

    public void myMethod() {
        service.myMethod();
    }
}

//4) Like Spring's beans.xml
public class MyModule implements Module {
    public void configure(Binder binder) {
        binder.bind(MyService.class).to(MyServiceImpl.class).in(Scopes.SINGLETON);
    }
}

//5) 
public class Test {
    public static void main(String[] args) {
        //IoC Container, the heart of the Guice framework
        Injector injector = Guice.createInjector(new MyModule());
        
        Client client = injector.getInstance(Client.class);
        client.myMethod();
    }
}


Usage 2:
//1)
@ImplementedBy(MyServiceImpl.class)
public interface MyService {
    void myMethod();
}

//2)
public class MyServiceImpl implements MyService {
    public void myMethod() {
        System.out.println("Hello, World!");
    }
}

//3)
public class Client {
    @Inject
    private MyService service;

    public void myMethod() {
        service.myMethod();
    }
}

//4)
public class MyModule implements Module {
    public void configure(Binder binder) {}
}

//5) Test


Note: Some code snippet are from Robbin's Blog.
分享到:
评论

相关推荐

    guice-4.0-API文档-中文版.zip

    赠送jar包:guice-4.0.jar; 赠送原API文档:guice-4.0-javadoc.jar; 赠送源代码:guice-4.0-sources.jar; 赠送Maven依赖信息文件:guice-4.0.pom; 包含翻译后的API文档:guice-4.0-javadoc-API文档-中文(简体)版...

    guice.jar/guice.jar

    guice.jar guice.jar guice.jar guice.jar guice.jar guice.jar guice.jar

    基于guice的简单项目

    **基于Guice的简单项目** 在Java开发中,依赖注入(Dependency Injection,简称DI)是一种设计模式,它有助于降低代码间的耦合度,提高代码的可测试性和可维护性。Guice是Google提供的一款轻量级的DI框架,它简化了...

    Guice用户中文指南

    ### Guice用户中文指南 #### 一、简介 Guice是一个专门为Java 5及后续版本设计的超轻量级依赖注入框架。它旨在简化应用程序组件之间的依赖管理,并提供了一个更为简洁、灵活的方式来处理对象间的耦合关系。Guice的...

    google Guice 1.0 用户指南 中文

    "google Guice 1.0 用户指南 中文" Guice 是一个超轻量级的、下一代的、为 Java 5 及后续版本设计的依赖注入容器。它可以帮助 Java 企业应用开发社区解决连接对象方面的问题,例如 Web 应用如何访问中间层服务、...

    Guice 中文文档 例子

    **Guice 概述** Guice 是 Google 推出的一款轻量级的依赖注入框架,专为 Java 5 及其后续版本设计。依赖注入(Dependency Injection,简称 DI)是一种设计模式,它允许开发者在不直接创建对象的情况下,将依赖关系...

    guice超轻量级依赖注入

    Guice,全称为Google Guice,是一款由Google开发的轻量级依赖注入(Dependency Injection,简称DI)框架,主要用于简化Java应用的初始化和组件管理。依赖注入是一种设计模式,它可以帮助开发者解耦代码,提高软件的...

    guice-multibindings-3.0-API文档-中文版.zip

    赠送jar包:guice-multibindings-3.0.jar; 赠送原API文档:guice-multibindings-3.0-javadoc.jar; 赠送源代码:guice-multibindings-3.0-sources.jar; 赠送Maven依赖信息文件:guice-multibindings-3.0.pom; ...

    guice-3.0.rar

    Guice-3.0是Guice的一个版本,包含了核心库guice-3.0.jar,以及与Spring和Struts2集成的扩展库guice-spring-3.0.jar和guice-struts2-plugin-3.0.jar。 1. **Guice核心概念**: - **依赖注入**:Guice的核心机制,...

    guice-3.0-API文档-中英对照版.zip

    赠送jar包:guice-3.0.jar; 赠送原API文档:guice-3.0-javadoc.jar; 赠送源代码:guice-3.0-sources.jar; 赠送Maven依赖信息文件:guice-3.0.pom; 包含翻译后的API文档:guice-3.0-javadoc-API文档-中文(简体)-...

    google guice 3.0源码

    Google Guice,全称为GoogleInject,是一个轻量级的依赖注入框架,由Google开发并开源。Guice的目标是简化Java应用程序的构造和管理,通过自动装配对象依赖关系,让开发者可以专注于业务逻辑而不是对象的创建和组装...

    Google Guice: Agile Lightweight Dependency Injection Framework

    ### Google Guice: 敏捷轻量级依赖注入框架详解 #### 一、引言与背景 在现代软件开发中,依赖注入(Dependency Injection, DI)已成为构建灵活、可维护和可测试应用程序的重要手段之一。Google Guice作为一款100%...

    guice-3.0-API文档-中文版.zip

    赠送jar包:guice-3.0.jar; 赠送原API文档:guice-3.0-javadoc.jar; 赠送源代码:guice-3.0-sources.jar; 赠送Maven依赖信息文件:guice-3.0.pom; 包含翻译后的API文档:guice-3.0-javadoc-API文档-中文(简体)版...

    shiro,guice集成

    ### Apache Shiro 与 Guice 集成详解 #### 概述 在现代软件开发过程中,集成不同的框架和技术是常见的需求。Apache Shiro 是一个强大的、易用的 Java 安全框架,提供了认证、授权、加密和会话管理功能。而 Google ...

    guice-multibindings-3.0-API文档-中英对照版.zip

    赠送jar包:guice-multibindings-3.0.jar; 赠送原API文档:guice-multibindings-3.0-javadoc.jar; 赠送源代码:guice-multibindings-3.0-sources.jar; 赠送Maven依赖信息文件:guice-multibindings-3.0.pom; ...

    guice-assistedinject-3.0-API文档-中英对照版.zip

    赠送jar包:guice-assistedinject-3.0.jar; 赠送原API文档:guice-assistedinject-3.0-javadoc.jar; 赠送源代码:guice-assistedinject-3.0-sources.jar; 赠送Maven依赖信息文件:guice-assistedinject-3.0.pom;...

    goole-guice所有JAR包

    Guice,全称为Google Guice,是Google推出的一款轻量级的依赖注入(Dependency Injection,简称DI)框架,专门用于简化Java应用中的对象组装和管理。依赖注入是一种设计模式,它可以帮助开发者降低代码间的耦合,...

    google-guice用户手册

    ### Google Guice 用户手册知识点详解 #### 一、Google Guice 概览 **Google Guice** 是一个轻量级的 Java 依赖注入容器,它为 Java 5 及以上版本提供支持。与传统的对象创建方式相比,Guice 通过减少样板代码...

    Google Guice需要的jar

    Google Guice是一个轻量级的依赖注入框架,由Google开发并维护,主要用于简化Java应用程序的构建和管理。依赖注入(Dependency Injection,简称DI)是一种设计模式,它可以帮助开发者减少代码间的耦合,提高代码的可...

Global site tag (gtag.js) - Google Analytics