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

Google Guice Provider Binding

阅读更多

    Google Guice Provider Binding提供了更加灵活的依赖注入,根据用户特定需求绑定特定依赖实现,主要有两种方式,第一种是@Provides注解方式,第二种是自定义Provider实现Provider接口的方式。

 

    一。ChatDao.java

 

package com.template.chat;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 8/4/11
 * Time: 4:39 PM
 * To change this template use File | Settings | File Templates.
 */
public interface ChatDao {

    void send(String message);

    String receive();
}

 

 

     二。ChatDaoImpl.java

 

package com.template.chat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 8/4/11
 * Time: 4:39 PM
 * To change this template use File | Settings | File Templates.
 */
public class ChatDaoImpl implements ChatDao {
    private Logger logger = LoggerFactory.getLogger(ChatDaoImpl.class);

    @Override
    public void send(String message) {
        logger.info(message);
    }

    @Override
    public String receive() {
        return "receive message content!";
    }
}

 

 

     三。ChatService.java

 

package com.template.chat;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 8/4/11
 * Time: 4:42 PM
 * To change this template use File | Settings | File Templates.
 */
public interface ChatService {

    void send(String message);

    String receive();
}

 

 

      四。ChatServiceImpl.java

 

package com.template.chat;

import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 8/4/11
 * Time: 4:43 PM
 * To change this template use File | Settings | File Templates.
 */
public class ChatServiceImpl implements ChatService {
    private Logger logger = LoggerFactory.getLogger(ChatServiceImpl.class);

    @Inject
    private ChatDao chatDao;

    @Override
    public void send(String message) {
        logger.info("Sending a message!");
        chatDao.send(message);
        logger.info("Sended a message!");
    }

    @Override
    public String receive() {
        logger.info("Received a message!");
        return chatDao.receive();
    }
}

 

      五。Main.java

 

package com.template.chat;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 8/4/11
 * Time: 4:59 PM
 * To change this template use File | Settings | File Templates.
 */
public class Main {
    private static Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {
        Module module = new ChatModule();
        Injector injector = Guice.createInjector(module);
        ChatService chatService = injector.getInstance(ChatService.class);
        chatService.send("Hello,my name is Zhong Gang!");
        String receive = chatService.receive();
        logger.info(receive);
    }
}

 

 

       六。@Provides注解实现方式

 

package com.template.chat;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 8/4/11
 * Time: 4:53 PM
 * To change this template use File | Settings | File Templates.
 */
public class ChatModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(ChatService.class).to(ChatServiceImpl.class);
    }

    @Provides
    protected ChatDao provideChatDao() {
        ChatDao chatDao = new ChatDaoImpl();
        return chatDao;
    }
}

 

       providerone

 

      七。自定义Provider实现方式

 

      ChatDaoProvider.java

 

package com.template.chat;

import com.google.inject.Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 8/4/11
 * Time: 5:18 PM
 * To change this template use File | Settings | File Templates.
 */
public class ChatDaoProvider implements Provider<ChatDao> {
    private Logger logger = LoggerFactory.getLogger(ChatDaoProvider.class);

    @Override
    public ChatDao get() {
        logger.info("ChatDaoProvider is customize provider!");
        return new ChatDaoImpl();
    }
}

 

       ChatModule.java

 

package com.template.chat;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 8/4/11
 * Time: 4:53 PM
 * To change this template use File | Settings | File Templates.
 */
public class ChatModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(ChatService.class).to(ChatServiceImpl.class);

        bind(ChatDao.class).toProvider(ChatDaoProvider.class);
    }
}

 

      providertwo

  • 大小: 15.3 KB
  • 大小: 12.1 KB
1
3
分享到:
评论

相关推荐

    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

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

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

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

    Google Guice需要的jar

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

    google guice 3.0源码

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

    google-guice

    Guice的核心概念包括模块(Module)、注解(Annotation)和绑定(Binding)。模块是Guice配置的载体,它定义了哪些类将被实例化以及它们之间的依赖关系。开发者可以创建自定义模块,并通过`@Provides`注解的方法来...

    google guice基础例子

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

    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(转)

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

    Google的产品Guice

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

    guice.jar/guice.jar

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

    goole-guice所有JAR包

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

    guice超轻量级依赖注入

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

    guice-3.0.rar

    Guice是Google开发的一款轻量级的Inversion of Control(IoC)容器,它通过依赖注入(Dependency Injection,DI)来管理对象的生命周期和装配。Guice-3.0是Guice的一个版本,包含了核心库guice-3.0.jar,以及与...

Global site tag (gtag.js) - Google Analytics