zoukankan      html  css  js  c++  java
  • Guice2.0的变化——第一部分 新的特性(上)

    http://superleo.iteye.com/blog/314816

    Private Modules

    PrivateModules 用于创建并不需要对外可见的绑定对象。当然,这样会使得封装变得更加简单,还避免了冲突。

    作者没有写关于 PrivateModules 的内容,有可能还没有更新完吧。我们就简单看一个例子:

    Java代码  
    1. public class FooBarBazModule extends PrivateModule {  
    2.    protected void configurePrivateBindings() {  
    3.      bind(Foo.class).to(RealFoo.class);  
    4.      expose(Foo.class); //expose方法用于向外部暴露内部的绑定Foo  
    5.   
    6.      install(new TransactionalBarModule()); //install方法允许将本PrivateModule模块嵌入到TransactionalBarModule模块中去。  
    7.      expose(Bar.class).annotatedWith(Transactional.class); //expose方法用于向外部暴露内部的绑定Bar,并令其标注为Transactional  
    8.   
    9.      bind(SomeImplementationDetail.class);  
    10.      install(new MoreImplementationDetailsModule());  
    11.    }  
    12.   
    13.     //向外部暴露的方法  
    14.    @Provides @Exposed  
    15.    public Baz provideBaz() {  
    16.      return new SuperBaz();  
    17.    }  
    18.  }  
    com.google.inject.PrivateModule

    A module whose configuration information is hidden from its environment by default. Only bindings that are explicitly exposed will be available to other modules and to the users of the injector. This module may expose the bindings it creates and the bindings of the modules it installs.

    A private module can be nested within a regular module or within another private module using install(). Its bindings live in a new environment that inherits bindings, type converters, scopes, and interceptors from the surrounding ("parent") environment. When you nest multiple private modules, the result is a tree of environments where the injector's environment is the root.

    Guice EDSL bindings can be exposed with expose(). @Provides bindings can be exposed with the @Exposed annotation:

     public class FooBarBazModule extends PrivateModule {
       protected void configure() {
         bind(Foo.class).to(RealFoo.class);
         expose(Foo.class);
    
         install(new TransactionalBarModule());
         expose(Bar.class).annotatedWith(Transactional.class);
    
         bind(SomeImplementationDetail.class);
         install(new MoreImplementationDetailsModule());
       }
    
       @Provides @Exposed
       public Baz provideBaz() {
         return new SuperBaz();
       }
     }
     

    Private modules are implemented using parent injectors. When it can satisfy their dependencies, just-in-time bindings will be created in the root environment. Such bindings are shared among all environments in the tree.

    The scope of a binding is constrained to its environment. A singleton bound in a private module will be unique to its environment. But a binding for the same type in a different private module will yield a different instance.

    A shared binding that injects the Injector gets the root injector, which only has access to bindings in the root environment. An explicit binding that injects the Injector gets access to all bindings in the child environment.

    To promote a just-in-time binding to an explicit binding, bind it:

       bind(FooImpl.class);
     
    Since:
    2.0
    Author:
    jessewilson@google.com (Jesse Wilson)
  • 相关阅读:
    sql 生成开始日期到结束日期相差天数或日期
    自定义表做存储过程变量
    [转]html 移动互联网终端的javascript touch事件,touchstart, touchend, touchmove
    [转]JQuery.Ajax之错误调试帮助信息
    解决IOS safari在input focus弹出输入法时不支持position fixed的问题
    查看 存储过程的执行时间
    ListView
    android矩阵详解
    跳出圈子 “莫忘初心,方得始终”
    Eclipse使用
  • 原文地址:https://www.cnblogs.com/ydxblog/p/7985825.html
Copyright © 2011-2022 走看看