Java 将 EJB 注入 servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18317544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Injecting EJB into servlet
提问by Leos Literak
I googled without luck trying to understand why Weblogic 10.3.4 does not inject EJB into annoted field in servlet.
我用谷歌搜索但没有运气试图理解为什么 Weblogic 10.3.4 没有将 EJB 注入 servlet 中的注释字段。
Ear contains ejb.jar defining DAO EJB and web.war with TestServlet.
Ear 包含用 TestServlet 定义 DAO EJB 和 web.war 的 ejb.jar。
PluginDataDAO.java
插件DataDAO.java
@Stateless
public class PluginDataDAO implements IPluginDataDAO {
}
IPluginDataDAO.java
IPluginDataDAO.java
@Local
public interface IPluginDataDAO {
}
TestServlet.java
测试服务程序
public class TestServlet extends HttpServlet {
@EJB(mappedName = "PluginDataDAO")
private IPluginDataDAO pluginDataDAO;
}
web.xml
网页.xml
<web-app version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID">
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>cz.literak.blog.j2ee.TestServlet</servlet-class>
</servlet>
Servlet is inside web.war, EJB in ejb.jar. I tried the annotation with/without mapped name attribute without luck. When I tried to upgrade web.xml to 3.0, deployment failed that 3.0 is not enumerated. What is wrong? Why is pluginDataDAO still null? Thank you.
Servlet 在 web.war 中,EJB 在 ejb.jar 中。我在没有运气的情况下尝试了带/不带映射名称属性的注释。当我尝试将 web.xml 升级到 3.0 时,部署失败,未枚举 3.0。怎么了?为什么 pluginDataDAO 仍然为空?谢谢你。
采纳答案by Leos Literak
Following combination works:
以下组合作品:
Servlet
小服务程序
@EJB
private IPluginDataDAO pluginDataDAO;
web.xml
网页.xml
...
<ejb-local-ref>
<ejb-ref-name>PluginDataDAO</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>cz.literak.blog.j2ee.dao.IPluginDataDAO</local>
</ejb-local-ref>
...
I thought that adding references to web.xml is not neccessary .. What are the rules, when to add them?
我认为添加对 web.xml 的引用不是必需的 .. 规则是什么,何时添加?
回答by Mike Braun
As for the Servlet 3 issue; WebLogic 10.3.x is a Java EE 5 implementation meaning it only supports Servlet 2.5.
至于 Servlet 3 问题;WebLogic 10.3.x 是 Java EE 5 实现,这意味着它仅支持 Servlet 2.5。
The example should work though. Maybe try a completely new project with only that Servlet and EJB in it.
这个例子应该可以工作。也许尝试一个只有那个 Servlet 和 EJB 的全新项目。
Also try the same code with the latest WebLogic 12.1.2. It can be downloaded for free at the Oracle site.
还可以使用最新的 WebLogic 12.1.2 尝试相同的代码。它可以在 Oracle 站点上免费下载。
回答by user3122855
I had the same problem and solved it with @ManagedBean
:
我遇到了同样的问题并用@ManagedBean
以下方法解决了它:
@ManagedBean
public class TestServlet extends HttpServlet {
@EJB(mappedName = "PluginDataDAO")
private IPluginDataDAO pluginDataDAO;
回答by BatBold
I think there is very good answer in this link...Injecting a stateless EJB into Servlet...
我认为此链接中有很好的答案... Injecting a stateless EJB into Servlet...
this guy Balus is saying you are trying to use DI in constructor which is not correct...earliest you can set it is in init() ....just copied the answer hoping someone else would find usefull
这个家伙 Balus 说你试图在构造函数中使用 DI,这是不正确的......你最早可以在 init() 中设置它......只是复制了答案,希望其他人会觉得有用