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; } }
七。自定义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); } }