`
Wind_ZhongGang
  • 浏览: 261558 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Google Guice 小试牛刀

阅读更多

  Google Guice是一个轻量级Dependency Injection依赖注入框架,能够提供动态注入,即当不知道注射的这个对象注射给谁呢,需要在运行时才能得到的的这个接口的实现,这是Spring DI所不具有的,Spring DI所有配置都是写死的,并且Spring DI在应用程序启动时所有依赖注入关系都会初始好,而Google Guice则可以根据需要进行依赖注入初始化,也就是说只有当需要时,就可以对依赖注入关系进行初始化。

 

  引入Google Guice包,从这个网址可以下载http://google-guice.googlecode.com/files/guice-3.0.zip

 

  一。CommentDao.java

 

package com.template.guice;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-8-2
 * Time: 下午9:37
 */
public interface CommentDao {

    public void comment(String message);
}

 

  二。CommentDaoImpl.java

 

package com.template.guice;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-8-2
 * Time: 下午9:38
 */
public class CommentDaoImpl implements CommentDao {

    public CommentDaoImpl() {
    }

    @Override
    public void comment(String message) {
        System.out.print(message);
    }
}

 

  三。CommentService.java

 

package com.template.guice;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-8-2
 * Time: 下午9:39
 */
public interface CommentService {

    public void comment();
}

 

  四。CommentServiceImpl.java

 

package com.template.guice;

import com.google.inject.Inject;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-8-2
 * Time: 下午9:39
 */
public class CommentServiceImpl implements CommentService {

    private CommentDao commentDao;

    @Inject
    public CommentServiceImpl(CommentDao commentDao) {
        this.commentDao = commentDao;
    }

    @Override
    public void comment() {
        commentDao.comment("This is a comment message!");
    }

    public void setCommentDao(CommentDao commentDao) {
        this.commentDao = commentDao;
    }
}

 

  五。CommentModule.java

 

package com.template.guice;

import com.google.inject.AbstractModule;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-8-2
 * Time: 下午9:46
 */
public class CommentModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(CommentDao.class).to(CommentDaoImpl.class);
    }
}
 

 

  六。Main.java

 

package com.template.guice;

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

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-8-2
 * Time: 下午9:55
 */
public class Main {

    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new CommentModule());
        CommentService commentService = injector.getInstance(CommentServiceImpl.class);
        commentService.comment();
    }
}

 

  @Inject表示Guice会隐式调用CommentServiceImpl的构造方法,而CommentModule则表示需要将CommentDao以CommentDaoImpl来注入对象中。

2
3
分享到:
评论
6 楼 bastengao 2011-08-03  
Wind_ZhongGang 写道
发现了,Google Guice提供了一个guice-spring-3.0.jar,使用这个包可以把我们的Spring配置转换成Google Guice

嗯,我也找到了,guice 提供的spring 插件可以将spring的bean注入到guice中。但我的目的可否将Guice的bean注入到了spring mvc中的Controller?
5 楼 Wind_ZhongGang 2011-08-03  
可以直接去http://code.google.com/p/google-guice/这个上面看,不过是英文的
4 楼 cantellow 2011-08-03  
听说guice性能很好,楼主有木有这方面的资料
3 楼 Wind_ZhongGang 2011-08-03  
发现了,Google Guice提供了一个guice-spring-3.0.jar,使用这个包可以把我们的Spring配置转换成Google Guice
2 楼 Wind_ZhongGang 2011-08-03  
呵呵,我也是刚开始关注这个东西,至于你说的整合,我想还得继续深入研究下才能找到解决方法。不过感兴趣的话,咱们好友聊,共同进步嘛,哈哈。
1 楼 bastengao 2011-08-03  
支持一下,我也很喜欢guice.一直想在产品中使用,想让guice与spring mvc整合,不知道楼主有什么解决方法?

相关推荐

    google Guice 1.0 用户指南 中文

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

    DI容器框架Google Guice与Spring框架的区别

    "DI容器框架Google Guice与Spring框架的区别" 这个标题表明我们要探讨的是两种不同的依赖注入(Dependency Injection,简称DI)容器——Google Guice和Spring框架之间的差异。DI是一种设计模式,它帮助开发者在对象...

    Google Guice与MyBatis集成,并实现发送邮件轮询

    Google Guice 这个高效的与Spring类似的依赖注入框架; MyBatis配置和使用; Google Guice与MyBatis集成,支持注解事务,简单的无法想象; Mybatis与mysql集成;实现发送邮件轮询; 源码是个web项目,里面有数据库的...

    Google guice

    **Google Guice**,全称为Google Injection,是一个轻量级的依赖注入框架,它通过注解(Annotations)来实现对象的自动装配,简化了Java应用的构造和管理。Guice的核心理念是帮助开发者摆脱手动创建对象和管理对象...

    Google Guice需要的jar

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

    google guice 3.0源码

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

    google guice基础例子

    Guice是Google开发的一个轻量级,基于Java5(主要运用泛型与注释特性)的依赖注入框架(IOC)。Guice非常小而且快。Guice是类型安全的,它能够对构造函数,属性,方法(包含任意个参数的任意方法,而不仅仅是setter...

    google-guice

    谷歌Guice,全名Google Guice,是一款轻量级的依赖注入框架,专为Java 5及更高版本设计。依赖注入(Dependency Injection,简称DI)是一种软件设计模式,旨在降低代码间的耦合度,提高可测试性和可维护性。Guice通过...

    Learning Google Guice

    谷歌Guice开发英文文档,很详细,对开发很有帮助,可当成工具书使用!

    Google Guice: Agile Lightweight Dependency Injection Framework

    Google Guice: Agile Lightweight Dependency Injection Framework will not only tell you "how," it will also tell you "why" and "why not," so that all the knowledge you gain will be as widely applicable ...

    google-guice用户手册

    google-guice用户手册,据说和spring pk

    Google Guice入世(转 附带一Guice1.0的简单测试代码)

    博文链接:https://avengerbevis.iteye.com/blog/69237

    guice.jar/guice.jar

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

    Google的产品Guice

    用户指南 博文链接:https://hejianjie.iteye.com/blog/83374

    初试Guice(转)

    Guice,全称Google Guice,是一款轻量级的依赖注入(Dependency Injection,简称DI)框架,由Google开发并开源。它主要用于简化Java应用程序的构造和管理,通过DI来解耦代码,使代码更易于测试、维护和扩展。Guice的...

    Apress.Google.Guice.Agile.Lightweight.Dependency.Injection.Framework.Apr.2008

    《Apress.Google.Guice.Agile.Lightweight.Dependency.Injection.Framework.Apr.2008》这本书专注于介绍Google Guice这一轻量级依赖注入框架。依赖注入(Dependency Injection,简称DI)是软件设计模式中的一种,它...

    Guice中文文档

    Guice中文文档,介绍Guice的基本使用,适合初学者。

    guice超轻量级依赖注入

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

Global site tag (gtag.js) - Google Analytics