为什么 CDI 注入在某些模块中不起作用,而在其他模块中不起作用?
在我的 Java EE 项目中,有几个“Java EE”模块和一个 Web 模块。 其中一个 Java EE 模块向 CDI 提供了一个可供其他模块使用的类:
@ApplicationScoped
public class XFactory {
@Produces @Actual
public X create() {
return new X();
}
}
它们被注入到
@SessionScoped
public class Target implements Serializable {
X x;
@Inject
public void setX(@Actual X x){
this.x = x;
}
}
但是,这只适用于其中一个 Java EE 模块和 Web 模块。在所有剩余的 Java EE 模块中,注入始终失败,我不知道为什么:我得到的只是WELD-1408,不满足的依赖关系。
所有模块在适当的位置都有 beans.xml
,只要我不切换到注入,它们都可以工作。大多数目标 Bean 已在 JSF 中用作注入 Bean。 Java EE 模块的特殊之处在于,bean 被注入到 Web 模块中的 servlet 中,而不是 JSF 中。
该项目在 GlassFish 3.1 中使用 Java EE 6、EJB 3.1 运行。依赖关系由 Maven 3 管理。X
本身是Serialized
,以满足钝化范围。
您以前遇到过这个吗?我可能做错了什么?
更新:上面添加了依赖管理备注。
更新:更正了Target
中@Actual
的位置。
更新:经过一天的实验后,用更多细节更新了描述。
In my Java EE project, there are several "Java EE" modules and a web module.
One of the Java EE modules provides a class to CDI that is to be used by the other modules:
@ApplicationScoped
public class XFactory {
@Produces @Actual
public X create() {
return new X();
}
}
They are injected into
@SessionScoped
public class Target implements Serializable {
X x;
@Inject
public void setX(@Actual X x){
this.x = x;
}
}
However, this works only in one of the Java EE modules and in the web module. In all of the remaining Java EE modules, injection consistently fails, and I am clueless as to why: All I get is WELD-1408, unsatisfied dependency.
All of the modules have beans.xml
in the proper places, they all work as long as I don't switch to injection. Most of the target beans are already in use as injected beans in a JSF.
What's special about the Java EE module that works is that the bean is injected into a servlet in the web module, not the JSF.
The project runs with Java EE 6, EJB 3.1 in GlassFish 3.1. Dependencies are managed by Maven 3. X
itself is Serializable
, to satisfy the passivating scopes.
Did you come across this before? What could I have done wrong?
Update: Added dependency management remark above.
Update: Corrected the position of @Actual
in Target
.
Update: Updated the description with more details after a day of experiments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎是 Glassfish 3.1、其包含的库之一或 JDK 6 中的问题。
我刚刚将系统更新到 Glassfish 3.1.1 和 JDK 7,问题不再出现。
This appears to be a problem in Glassfish 3.1, in one of its included libraries, or possibly in JDK 6.
I have just updated my system to Glassfish 3.1.1 and JDK 7, and problem does not occur any longer.
根据我最近使用 Weblogic 的经验,我发现包含下划线的命名空间会阻止 EJB 模块注入任何 Bean。
我建议您也尝试一下 Glassfish。
问候!
In my recent experience on Weblogic I discovered that namespaces containing underscores prevents EJB modules to get any beans injected.
I suggest you to give a try on Glassfish too.
Regards!