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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值