Injecting EJB into servlet
dependency-injection, ejb, java, servlets, weblogic
Solution
Following combination works:
Servlet
@EJB
private IPluginDataDAO pluginDataDAO;
web.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?
Problem
I googled without luck trying to understand why Weblogic 10.3.4 does not inject EJB into annoted field in servlet. Ear contains ejb.jar defining DAO EJB and web.war with TestServlet. PluginDataDAO.java ``` @Stateless public class PluginDataDAO implements IPluginDataDAO { } ``` IPluginDataDAO.java ``` @Local public interface IPluginDataDAO { } ``` TestServlet.java ``` public class TestServlet extends HttpServlet { @EJB(mappedName = "PluginDataDAO") private IPluginDataDAO pluginDataDAO; } ``` web.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.