SEAM:组件“解​​除注入” “太快了”在拦截器中?

发布于 2024-08-14 16:56:52 字数 663 浏览 2 评论 0原文

假设我在 SEAM 应用程序中有以下拦截器:

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() 有效(因此 Monitor 被正确注入),myMonitor.b() 失败,因为 Monitor 已经为空。 Seam 文档说:“方法完成并退出后,注入的值立即被取消注入(即设置为 null)。”

这就是正在发生的事情吗?我可以做些什么来告诉 SEAM“尚未”“清除”组件吗?我当然也可以做类似 XContext.get(..) 的事情,但我想知道这是一个错误还是我这边的错误。谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画尸师 2024-08-21 16:56:52

试试这个吧

Object response = null;

try {
    myMonitor.a();

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

return response;

Try this one instead

Object response = null;

try {
    myMonitor.a();

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

return response;

regards,

怎会甘心 2024-08-21 16:56:52

避免使用注射。

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

效果很好!
这是链接

Avoid using injection.

Try working around this problem. I see you have some sort of monitoring going on. Look at this interceptor that captures the amount of time a method is executed in Seam components. Try modifying your code to match that.

It works great!
Here is the link

荆棘i 2024-08-21 16:56:52

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() 返回之前销毁它,从而为您留下对损坏代理的引用。最好的建议是了解您所保留的内容的范围和生命周期,因为您“生活在边缘”。

Seam is working as advertised.

You could just ignore the disinjection:

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
     }
  }
}

The caveat here is that Seam is disinjecting the reference for a reason. Seam wants to control the lifecycle and identity of "myMonitor" and by keeping a reference to it, you are not abiding by your contract with Seam. This could lead to unexpected behavior.

For instance, if myMonitor were for some reason in the Stateless scope, Seam might destroy it before ctx.proceed() returns, leaving you with a reference to a broken proxy. Best advice is to know the scope and lifecycle of what you are retaining since you are "living on the edge."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文