1. 程式人生 > >Guice2.0的變化——第一部分 新的特性(上)

Guice2.0的變化——第一部分 新的特性(上)

xposed lsmod ide his leo access cep within mat

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. install(new TransactionalBarModule()); //install方法允許將本PrivateModule模塊嵌入到TransactionalBarModule模塊中去。
  6. expose(Bar.class).annotatedWith(Transactional.class); //expose方法用於向外部暴露內部的綁定Bar,並令其標註為Transactional
  7. bind(SomeImplementationDetail.class);
  8. install(new MoreImplementationDetailsModule());
  9. }
  10. //向外部暴露的方法
  11. @Provides @Exposed
  12. public Baz provideBaz() {
  13. return new SuperBaz();
  14. }
  15. }

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:
[email protected] (Jesse Wilson)

Guice2.0的變化——第一部分 新的特性(上)