Guice学习笔记(一) 简单示例

  1. package com.ohergal.gtest;
  2. /**
  3.  * Created by IntelliJ IDEA.
  4.  * User: Administrator
  5.  * Date: 2008-5-27
  6.  * Time: 11:30:29
  7.  * To change this template use File | Settings | File Templates.
  8.  */
  9. public interface IMyService {
  10.     public boolean vilidateMySelf(String s);

  11. }




  1. package com.ohergal.gtest;
  2. /**
  3.  * Created by IntelliJ IDEA.
  4.  * User: Administrator
  5.  * Date: 2008-5-27
  6.  * Time: 11:31:39
  7.  * To change this template use File | Settings | File Templates.
  8.  */
  9. public class MyServiceImp implements IMyService{
  10.      public boolean vilidateMySelf(String s){
  11.            System.out.println("comto test");
  12.            return true;
  13.     }

  14. }





  1. package com.ohergal.gtest;
  2. import com.google.inject.Inject;
  3. /**
  4.  * Created by IntelliJ IDEA.
  5.  * User: Administrator
  6.  * Date: 2008-5-27
  7.  * Time: 14:17:11
  8.  * To change this template use File | Settings | File Templates.
  9.  */
  10. public class MyApp {
  11.     private IMyService service = null;
  12.     @Inject
  13.     public void setService(IMyService service){
  14.         this.service=service;   
  15.     }
  16.     public boolean go(){
  17.           return service.vilidateMySelf("test");
  18.     }

  19. }




  1. package com.ohergal.gtest;
  2. import com.google.inject.Module;
  3. import com.google.inject.Binder;
  4. import com.google.inject.Scopes;
  5. /**
  6.  * Created by IntelliJ IDEA.
  7.  * User: Administrator
  8.  * Date: 2008-5-27
  9.  * Time: 11:39:37
  10.  * To change this template use File | Settings | File Templates.
  11.  */
  12. public class MyModule implements Module {
  13.     public void configure(Binder binder) {
  14.         binder.bind(IMyService.class)
  15.           .to(MyServiceImp.class)
  16.           .in(Scopes.SINGLETON);
  17.     }
  18. }






单元测试代码
  1. package com.ohergal.gtest;
  2. import junit.framework.Test;
  3. import junit.framework.TestCase;
  4. import junit.framework.TestSuite;
  5. import com.google.inject.Module;
  6. import com.google.inject.Injector;
  7. import com.google.inject.Guice;
  8. /**
  9.  * Unit test for simple App.
  10.  */
  11. public class AppTest
  12.     extends TestCase
  13. {
  14.     /**
  15.      * Create the test case
  16.      *
  17.      * @param testName name of the test case
  18.      */
  19.     public AppTest( String testName )
  20.     {
  21.         super( testName );
  22.     }
  23.     /**
  24.      * @return the suite of tests being tested
  25.      */
  26.     public static Test suite()
  27.     {
  28.         return new TestSuite( AppTest.class );
  29.     }
  30.     /**
  31.      * Rigourous Test :-)
  32.      */
  33.     public void testApp()
  34.     {
  35.         MyApp app = new MyApp();
  36.         Module module = new MyModule();
  37.         Injector in = Guice.createInjector(module);
  38.         in.injectMembers(app);
  39.         //assertTrue( true );
  40.         assertTrue(app.go());
  41.     }
  42. }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值