Google Guice 小试牛刀--1

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

 

Java代码   收藏代码
  1. package com.template.guice;  
  2.   
  3. /** 
  4.  * Created by IntelliJ IDEA. 
  5.  * User: Zhong Gang 
  6.  * Date: 11-8-2 
  7.  * Time: 下午9:37 
  8.  */  
  9. public interface CommentDao {  
  10.   
  11.     public void comment(String message);  
  12. }  

 

  二。CommentDaoImpl.java

 

Java代码   收藏代码
  1. package com.template.guice;  
  2.   
  3. /** 
  4.  * Created by IntelliJ IDEA. 
  5.  * User: Zhong Gang 
  6.  * Date: 11-8-2 
  7.  * Time: 下午9:38 
  8.  */  
  9. public class CommentDaoImpl implements CommentDao {  
  10.   
  11.     public CommentDaoImpl() {  
  12.     }  
  13.   
  14.     @Override  
  15.     public void comment(String message) {  
  16.         System.out.print(message);  
  17.     }  
  18. }  

 

  三。CommentService.java

 

Java代码   收藏代码
  1. package com.template.guice;  
  2.   
  3. /** 
  4.  * Created by IntelliJ IDEA. 
  5.  * User: Zhong Gang 
  6.  * Date: 11-8-2 
  7.  * Time: 下午9:39 
  8.  */  
  9. public interface CommentService {  
  10.   
  11.     public void comment();  
  12. }  

 

  四。CommentServiceImpl.java

 

Java代码   收藏代码
  1. package com.template.guice;  
  2.   
  3. import com.google.inject.Inject;  
  4.   
  5. /** 
  6.  * Created by IntelliJ IDEA. 
  7.  * User: Zhong Gang 
  8.  * Date: 11-8-2 
  9.  * Time: 下午9:39 
  10.  */  
  11. public class CommentServiceImpl implements CommentService {  
  12.   
  13.     private CommentDao commentDao;  
  14.   
  15.     @Inject  
  16.     public CommentServiceImpl(CommentDao commentDao) {  
  17.         this.commentDao = commentDao;  
  18.     }  
  19.   
  20.     @Override  
  21.     public void comment() {  
  22.         commentDao.comment("This is a comment message!");  
  23.     }  
  24.   
  25.     public void setCommentDao(CommentDao commentDao) {  
  26.         this.commentDao = commentDao;  
  27.     }  
  28. }  

 

  五。CommentModule.java

 

Java代码   收藏代码
  1. package com.template.guice;  
  2.   
  3. import com.google.inject.AbstractModule;  
  4.   
  5. /** 
  6.  * Created by IntelliJ IDEA. 
  7.  * User: Zhong Gang 
  8.  * Date: 11-8-2 
  9.  * Time: 下午9:46 
  10.  */  
  11. public class CommentModule extends AbstractModule {  
  12.   
  13.     @Override  
  14.     protected void configure() {  
  15.         bind(CommentDao.class).to(CommentDaoImpl.class);  
  16.     }  
  17. }  
 

 

  六。Main.java

 

Java代码   收藏代码
  1. package com.template.guice;  
  2.   
  3. import com.google.inject.Guice;  
  4. import com.google.inject.Injector;  
  5.   
  6. /** 
  7.  * Created by IntelliJ IDEA. 
  8.  * User: Zhong Gang 
  9.  * Date: 11-8-2 
  10.  * Time: 下午9:55 
  11.  */  
  12. public class Main {  
  13.   
  14.     public static void main(String[] args) {  
  15.         Injector injector = Guice.createInjector(new CommentModule());  
  16.         CommentService commentService = injector.getInstance(CommentServiceImpl.class);  
  17.         commentService.comment();  
  18.     }  
  19. }  

 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值