使用 glassfish 为多个 Web 项目共享 ejb 3
我目前有一个包含 EJB 和 WAR 的 J2EE 项目。一切都很顺利,而我在最终的 EAR 中只捆绑了一个 WAR 和一个 EJB 模块。我现在需要使用相同的 EJB 模块开发另一个 WAR。
所以,我在新的WAR的库中添加了EJB模块。问题是,当我尝试部署与两个 WAR 和共享 EJB 捆绑在一起的 EAR 时,出现以下异常:
Caused by: java.lang.RuntimeException: Error while binding JNDI name com.alex.ejb.MyBeanRemote__3_x_Internal_RemoteBusinessHome__ for EJB : MyBean
当我从第二个 WAR 的库中删除 EJB 时,一切又恢复正常了。
有什么我错过的吗?
最后,真正的问题是“如何在多个 Web 应用程序中共享 EJB?”
感谢您的帮助
I currently have a J2EE project containing an EJB and a WAR. Everything goes fine while I have only one WAR and one EJB module bundled in the final EAR. I now need to develop an other WAR using the same EJB module.
So, I added the EJB module in the library of the new WAR. The problem is when I try to deploy the EAR bundled with the two WAR and the shared EJB, I get the following exception :
Caused by: java.lang.RuntimeException: Error while binding JNDI name com.alex.ejb.MyBeanRemote__3_x_Internal_RemoteBusinessHome__ for EJB : MyBean
As I remove the EJB from the library of the second WAR, everything work again.
Is there something I missed ?
Finally, the real question is "how to share an EJB within multiple web applications ?"
Thanks for help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为 bean 部署了两次,导致命名冲突。实际上,在使用 EAR 打包时,这不是您应该打包应用程序的方式。
不要将 EJB 模块放在 Web 模块的 WEB-INF/lib 文件夹中,您的 EAR 应具有以下结构:
资源
That's because the beans get deployed twice, causing a naming collision. Actually, that's just not how you should package your application when using an EAR packaging.
Don't put your EJB module(s) in the
WEB-INF/lib
folder of your Web modules, your EAR should have the following structure:Resources
您不需要在新的 WAR 中包含 EJB,您可以简单地使用 JNDI 或通过注入来查找它。在针对它进行编译方面,您可以创建一个 JAR,其中仅包含您需要使用的接口以及新项目的类路径。
You do not need to include the EJB in the new WAR, you can simply look it up using JNDI or by injecting it. In terms of compiling against it, you can create a JAR that simply contains the interface(s) you need to use, and that to the classpath of your new project.