`
yzd
  • 浏览: 1819475 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Guice笔记_补充

 
阅读更多
<p><strong>1、关于标注绑定,如若不是属性,是方法上的注入,如下适用标注</strong></p>
<p></p>
<p><textarea cols="50" rows="6" name="code" class="java">@Inject
@Www
public Service service;
//方法上的如下
@Inject
void injectService(@Www Service service) {
  ...
}</textarea></p>
<p></p>
<p></p>
<p><strong>2、隐式绑定</strong>,如果缺少显示绑定(即没有写任何binder),Guice会试图注入并创建一个所依赖的类的新实例,如果依赖于一个接口,Guice会寻找指向具体实现的@ImplementedBy标注,如果是依赖的是一个类,那就实例化此类。</p>
<p><textarea cols="50" rows="10" name="code" class="java">bind(ServiceImpl.class);
//自己绑定自己,当然这语句可有可无,对下面毫无影响
...
class Demo{
  @Inject
  Demo(ServiceImpl service){
    ...
  }
}</textarea></p>
<p></p>
<p><strong>3、定制Provide。</strong></p>
<p></p>
<p>有时候需要手工创建自己的对象,而不是让Guice创建他们,比如,一个来自第三方的实现类需要注入,但我们不能修改源码,不能添加@Inject标注,在这里就需要 自己定制Provider,手工创建对象。</p>
<p><textarea cols="66" rows="15" name="code" class="java">class WidgetProvider implements Provider&lt;Widget&gt; {

  final Service service;

  @Inject//这里注入了第三方实现类需要的参数
  WidgetProvider(Service service) {
    this.service = service;
  }

  public Widget get() {
    return new Widget(service);//这里自己手工传入参数,实例化
  }
}

</textarea></p>
<p></p>
<p>例子:与JNDI集成,我们需要绑定从JNDI得到的对象。</p>
<p><textarea cols="66" rows="15" name="code" class="java">class JndiProvider&lt;T&gt; implements Provider&lt;T&gt; {

  @Inject Context context;
  final String name;
  final Class&lt;T&gt; type;

  JndiProvider(Class&lt;T&gt; type, String name) {
    this.name = name;
    this.type = type;
  }

  public T get() {
    try {
      return type.cast(context.lookup(name));
    }
    catch (NamingException e) {
      throw new RuntimeException(e);
    }
  }

  /**
   * Creates a JNDI provider for the given
   * type and name.
   */
  static &lt;T&gt; Provider&lt;T&gt; fromJndi(
      Class&lt;T&gt; type, String name) {
    return new JndiProvider&lt;T&gt;(type, name);
  }
}
</textarea></p>
<p></p>
<p>我们可以使用定制的JndiProvider来将DataSource绑定到来自JNDI的一个对象上。</p>
<p><textarea cols="66" rows="7" name="code" class="java">// Bind Context to the default InitialContext.
bind(Context.class).to(InitialContext.class);

// Bind to DataSource from JNDI.
bind(DataSource.class)
    .toProvider(JndiProvider.fromJndi(DataSource.class, "..."));

</textarea></p>
<p></p>
<p></p>
<p><strong>4、绑定的作用域</strong></p>
<p></p>
<p>缺省情况下,Guice为每次注入创建一个新的对象,即“无作用域”</p>
<p>可以在配置绑定时指明作用域</p>
<p><textarea cols="68" rows="7" name="code" class="java">bind(MySingleton.class).in(Scopes.SINGLETON);
//或用标注,Guice缺省支持@Singleton;
@Singleton
class MySingleton {
  ...
}
//用in()的优先级高于标注

//可以使用Binder.bindScope()为定制的作用域 指定 标注.
//如,对于标注@SessionScoped 和 一个Scope的实现ServletScopes.SESSION;
binder.bindScope(SessionScoped.class,ServletScopes.SESSION);
</textarea></p>
<p></p>
<p></p>
<p><strong>创建作用域标注</strong></p>
<p></p>
<p>标注必须:</p>
<li>有一个 <span style="font-family: Courier New;">@Retention(RUNTIME)</span> 标注,从而使我们可以在运行时看到该标注。 </li>
<li>有一个 <span style="font-family: Courier New;">@Target({TYPE})</span> 标注。作用域标注只用于实现类。 </li>
<li>有一个 <span style="font-family: Courier New;">@ScopeAnnotation</span> 元标注。一个类只能使用一个此类标注。 </li>
<p><textarea cols="50" rows="8" name="code" class="java">/**
* Scopes bindings to the current transaction.
*/
@Retention(RUNTIME)
@Target({TYPE})
@ScopeAnnotation
public @interface TransactionScoped {} </textarea></p>
<p></p>
<p></p>
<p><strong>5、尽早加载绑定(类似Spring的layzload=false)</strong></p>
<p></p>
<p>有时你希望在启动时加载一个对象,可以通过以下方法实现初始化逻辑,通过在Guice必须首先初始化的单件对象上创造依赖关系来控制初始化顺序。</p>
<p><textarea cols="50" rows="15" name="code" class="java">bind(StartupTask.class).asEagerSingleton();

</textarea></p>
<p></p>
<p></p>
<p><strong>6、在不同作用域间注入</strong></p>
<ul>
<li>大作用域对象 -&gt;(注入) 小作用域对象或相同作用域的对象中 是安全的。</li>
<li>小作用域对象 -&gt;(注入) 大作用域对象时可能会出错,这时应该注入一个Provider&lt;T&gt;在需要时从较小的作用域中获取对象,并确保在T的作用域之外,永远不要调用这个提供者。</li>
</ul>
<p></p>
<p></p>
<p><strong>7、开发阶段</strong></p>
<p></p>
<p>Guice 明白你的应用开发需要经历不同的阶段。你可以在创建容器时告诉它应用程序运行在哪一个阶段。Guice 目前支持“开发”和“产品”两个阶段。我们发现测试通常属于其中某一个阶段。</p>
<ul>
<li>在开发阶段,Guice 会<span style="">根据</span>需要加载单件对象。这样,你的应用<span style="">程序</span>可以快速启动,只加载你正在测试的部分。</li>
<li>在产品阶段,Guice 会在启动时加载全部单件对象。这帮助你尽早捕获错误,提前优化性能。</li>
</ul>
<p></p>
<p><strong>8、拦截方法</strong></p>
<p><strong></strong></p>
<p>Guice 使用 <a title="AOP Alliance API" href="http://aopalliance.sourceforge.net/">AOP Alliance API</a>支持简单的方法拦截。可以在模块中使用 <span style="font-family: Courier New;">Binder 绑定拦截器。例如,</span>对标注有 <span style="font-family: Courier New;">@Transactional 的方法</span><span style="">应</span><span style="font-family: Courier New;">用</span><span style="">事务</span><span style="font-family: Courier New;">拦截器:</span></p>
<p><span style="font-family: Courier New;"><textarea cols="62" rows="15" name="code" class="java">import static com.google.inject.matcher.Matchers.*;

...

binder.bindInterceptor(
  any(),                              // Match classes.
  annotatedWith(Transactional.class), // Match methods.
  new TransactionInterceptor()        // The interceptor.
);

</textarea></span></p>
<p><span style="font-family: Courier New;">尽量让匹配代码多做些过滤工作,而不是在拦截器中过滤。因为匹配代码只在启动时运行一次。 </span>
</p>
<p><strong>10、Struts2支持</strong></p>
<p></p>
<p>要在 Struts 2.0.6 或更高版本中安装 Guice Struts 2插件,只要将 <span style="font-family: Courier New;">guice-struts2-plugin-1.0.jar</span> 包含在你的 Web 应用的classpath 中,并在 <span style="font-family: Courier New;">struts.xml </span><span style="">文件</span><span style="font-family: Courier New;">中</span>选择 Guice 作为你的 <span style="font-family: Courier New;">ObjectFactory</span> 实现即可:<br><br><span style="font-family: Courier New;"> &lt;constant name="struts.objectFactory" value="guice" /&gt;</span> </p>
<p></p>
<p>Guice 会注入所有你的 Struts 2 对象,包括动作和拦截器。你甚至可以设置动作类的作用域。你也可以在你的 <span style="font-family: Courier New;">struts.xml</span> 文件中指定 Guice 的<span style="font-family: Courier New;">Module</span>: </p>
<p></p>
<p><span style="font-family: Courier New;"> &lt;constant name="guice.module" value="mypackage.MyModule"/&gt;</span></p>
<p><span style="font-family: Courier New;"><br></span>如果你的所有绑定都是隐式的,你就根本不用定义模块了。</p>
<p><br></p>

<p></p>
<p></p>
<p><strong>9、可选注入</strong></p>
<p></p>
<p>有时你的代码应该在无论绑定是否存在的时候都能工作。在这些情况下,你可以使用 <span style="font-family: Courier New;">@Inject(optional=true)</span>,Guice 会在有绑定可用时,用一个绑定实现覆盖你的缺省实现。例如:<br><br><textarea cols="70" rows="1" name="code" class="java">@Inject(optional=true) Formatter formatter = new DefaultFormatter();</textarea><br>如果谁为 <span style="font-family: Courier New;">Formatter </span>创建了一个绑定,Guice 会基于该绑定注入实例。否则,如果 <span style="font-family: Courier New;">Formatter</span> 不能被注入(参见<em>隐式绑定</em>),Guice 会忽略可选成员。<br><br>可选注入只能应用于字段和方法,而不能用于构造<span style="">函数</span>。对于方法,如果一个参数的绑定找不到,Guice 就不会注入该方法,即便其他参数的绑定是可用的。</p>
<p></p>
<p><br></p>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics