高華的博客

          Eclipse能在Java占得一席之地
          隨筆 - 2, 文章 - 13, 評論 - 3, 引用 - 0
          數據加載中……

          初識Google Guice

          個人認為AOP其中的一個功能就是對Factory模式進行了改進。
          Guice當然也不例外,Guice是不需要寫配置文件的。
          實際上這里不寫,那里也得寫,總歸要寫。
          Guice需要寫Java類,
          import com.google.inject.Binder;
          import com.google.inject.Module;

          publicclass MyModule implements Module
          {

          publicvoid configure(Binder binder)
          {
          binder.bind(PrintService.
          class).to(PrintServiceImpl.class);
          }

          }
          代碼來自:http://blog.csdn.net/netHibernate/archive/2007/03/07/1523733.aspx
          運行前調用進行配置
          MyModule mo=new MyModule();
          Injector in=Guice.createInjector(mo);
          然后進行注入
          Client client=new Client();
          in.injectMembers(client);
          Guice通過Java Annotations控制注入

          publicclass Client
          {
          private String str="Hello world";
          private PrintService service;
          @Inject
          void injectPrintService(PrintService service)
          {
          this.service=service;
          }

          void printString()
          {
          service.print(str);
          }

          }
          嘿嘿,如果只能干這個,我還是寫Factory來得方便。
          Guice還有其他功能需要進一步研究。

          posted on 2007-06-01 07:18 高華 閱讀(86) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 钟山县| 永胜县| 阿巴嘎旗| 囊谦县| 盐源县| 新宁县| 邯郸县| 望奎县| 扶绥县| 大竹县| 体育| 新和县| 托克托县| 霞浦县| 通州市| 光泽县| 海宁市| 容城县| 天镇县| 通河县| 开化县| 斗六市| 鹤庆县| 奉化市| 会宁县| 盈江县| 抚顺县| 西和县| 江西省| 城固县| 敖汉旗| 陆丰市| 邯郸市| 嘉善县| 砚山县| 达尔| 张家川| 鄂伦春自治旗| 天津市| 集安市| 昌乐县|