SEAM:组件“解除注入” “太快了”在拦截器中?
假设我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个吧
,
Try this one instead
regards,
避免使用注射。
尝试解决这个问题。我看到你们正在进行某种监视。查看这个拦截器,它捕获 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
Seam 正在按照广告宣传的那样工作。
你可以忽略解除注入:
这里需要注意的是,Seam 解除引用是有原因的。 Seam 想要控制“myMonitor”的生命周期和身份,并且通过保留对它的引用,您就没有遵守与 Seam 签订的合同。这可能会导致意外的行为。
例如,如果 myMonitor 由于某种原因位于无状态范围内,Seam 可能会在 ctx.proceed() 返回之前销毁它,从而为您留下对损坏代理的引用。最好的建议是了解您所保留的内容的范围和生命周期,因为您“生活在边缘”。
Seam is working as advertised.
You could just ignore the disinjection:
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."