1

Let's say I have the following interceptor in a SEAM app:

public class MyInterceptor {
  @In
  private Monitor myMonitor;

  @AroundInvoke
  public Object aroundInvoke(InvocationContext ctx) throws Exception {
    try {
      myMonitor.a();
      return ctx.proceed();
    }
    finally {
      myMonitor.b();
    }
  }
}

myMonitor.a() works (so Monitor is correctly injected), myMonitor.b() fails because Monitor is already null. Seam Doc says: "Injected values are disinjected (i.e., set to null) immediately after method completion and outjection."

Is that what is happening? Can I do something to tell SEAM to "not yet" "disinject" the component? I can of course also do something like XContext.get(..), but I'm wondering whether this is a bug or a mistake from my side. thanks!

4

3 回答 3

1

试试这个

Object response = null;

try {
    myMonitor.a();

    response = ctx.proceed();
} finally {
    myMonitor.b();
}

return response;

问候,

于 2009-12-12T18:03:50.947 回答
1

避免使用注射。

尝试解决此问题。我看到你正在进行某种监控。看看这个拦截器,它捕获了一个方法在 Seam 组件中执行的时间量。尝试修改您的代码以匹配它。

效果很好!这是链接

于 2010-02-12T14:55:19.543 回答
0

Seam 正在像宣传的那样工作。

您可以忽略注入:

public class MyInterceptor {

  private Monitor myMonitor;

  @In
  private void setMonitor(Monitor aMonitor) {
     if (aMonitor != null) {
       myMonitor = aMonitor;
     }
  }


  @AroundInvoke
  public Object aroundInvoke(InvocationContext ctx) throws Exception {
    try {
      myMonitor.a();
      return ctx.proceed();
     }
     finally {
       myMonitor.b();
       myMonitor = null; //perform disinjection yourself
     }
  }
}

这里需要注意的是,Seam 出于某种原因去除了引用。Seam 想要控制“myMonitor”的生命周期和身份,并且通过保留对它的引用,您没有遵守与 Seam 的合同。这可能会导致意外行为。

例如,如果 myMonitor 出于某种原因处于无状态范围内,Seam 可能会在 ctx.proceed() 返回之前将其销毁,从而为您留下对损坏代理的引用。最好的建议是了解您保留的内容的范围和生命周期,因为您“生活在边缘”。

于 2010-02-19T18:24:19.547 回答