基类中的@Inject在基类方法中为null,在派生类中为ok
在下面的代码中,我尝试将 SessionScoped bean 注入无状态 EJB,但我想在 EJB 的抽象基类中执行 @Inject。根据 CDI 规范似乎表明这应该可行(绝不是浪费文字的规范):
“4.2.成员级元数据的继承
假设类X是由bean类直接或间接扩展的 托管 Bean 或会话 Bean Y 的。
如果 X 声明了一个注入字段 x,则 Y 继承 x。”
发生的情况是我可以访问继承的受保护成员 sessionView 来自 TestEjb,但不是来自基类中的代码。当我说“可以访问”时, 我的意思是注入的成员可以在运行时访问并且不为空。
@Named
@ViewAccessScoped
public class JsfBean extends implements Serializable {
@Inject private TestEjb ejb;
SessionView s1 = ejb.getSessionViewChild(); // sessionView injected ok
SessionView s2 = ejb.getSessionViewBase(); // sessionView is null
}
@Named
@SessionScoped
public class SessionView implements Serializable {}
@Stateless
public class TestEjb extends BaseClass implements Serializable {
public SessionView getSessionViewChild() {
return sessionView;
}
}
public abstract class BaseClass implements Serializable {
@Inject
protected SessionView sessionView;
public SessionView getSessionViewBase() {
return sessionView;
}
}
发生的情况是 s1 是有效的 SessionView 引用,但 s2 为 null。
我将 MyFaces CODI 1.01 与 Glassfish 3.1.1 中的 Weld 结合使用。我已经 尝试从 BaseClass 中删除抽象,甚至添加 @Named,但这还没有 有帮助,我看不出我做错了什么。
就目前情况来看,我必须从基类继承 sessionView 并且 将其作为参数传递回基类的方法。所以我将不胜感激,如果 任何人都可以指出我是否在做一些愚蠢的事情,也许这是 CODI/Weld 问题?
谢谢。
In the following code I'm trying to inject a SessionScoped bean into a stateless
EJB, but I want to do the @Inject in an abstract base class of the EJB. According to the
CDI spec it seems to suggest that this should work (never a spec to waste words):
"4.2. Inheritance of member-level metadata
Suppose a class X is extended directly or indirectly by the bean class
of a managed bean or session bean Y.If X declares an injected field x then Y inherits x."
What happens is that I can access the inherited protected member sessionView
from TestEjb, but not from code within the base class. When I say "can access",
I mean that the injected member is accessible at runtime and is not null.
@Named
@ViewAccessScoped
public class JsfBean extends implements Serializable {
@Inject private TestEjb ejb;
SessionView s1 = ejb.getSessionViewChild(); // sessionView injected ok
SessionView s2 = ejb.getSessionViewBase(); // sessionView is null
}
@Named
@SessionScoped
public class SessionView implements Serializable {}
@Stateless
public class TestEjb extends BaseClass implements Serializable {
public SessionView getSessionViewChild() {
return sessionView;
}
}
public abstract class BaseClass implements Serializable {
@Inject
protected SessionView sessionView;
public SessionView getSessionViewBase() {
return sessionView;
}
}
What happens is that s1 is a valid SessionView reference, but s2 is null.
I am using MyFaces CODI 1.01 in conjunction with Weld from Glassfish 3.1.1. I've
tried removing abstract from BaseClass and even added @Named, but this hasn't
helped and I can't see what I'm doing wrong.
As it stands it looks like I'll have to inherit sessionView from the base class and
pass it back in as a parameter to methods from the base class. So I'll be grateful if
anyone can point out if I'm doing something stupid, perhaps this is a CODI/Weld issue?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 Weld 中的一个错误。如果它与其他作用域一起工作,那么自定义作用域与 Weld 结合使用时会出现错误。
That's a bug in Weld. If it works with other scopes, it's a bug with custom scopes in combination with Weld.
事实证明,我在发帖时对问题的理解是基于
一组观察结果太有限——这都是错误的。事实证明,实际问题是我没有在接受泛型类型参数的重载方法中使用 @PersistenceContext 成功将实体管理器注入到无状态 ejb 中。这个问题据说已在 glassfish 3.1.2 中得到解决。例如:
find(T id) 中的 em 将为 null,并且使用 @EJB 注入与使用 @PersistenceContext 注入时存在相同的问题。真的是相当大啊!
http://java.net/jira/browse/GLASSFISH-17235
It turns out that my understanding of the problem at the time of posting was based on
too limited a set of observations - it was all wrong. It turns out that the actual problem was that I wasn't successfully injecting an entity manager into a stateless ejb with @PersistenceContext in an overloaded method that accepts a generic type parameter. This problem is supposedly fixed in glassfish 3.1.2. For example:
em in find(T id) will be null, and there's the same problem injecting with @EJB as with @PersistenceContext. Quite a biggy really!
http://java.net/jira/browse/GLASSFISH-17235