EJB 依赖注入到 Servelt - WAS7
我正在尝试使用 @EJB 注释注入 EJB:
- 当我将一个 EJB 注入另一个 EJB 时,它工作正常。
- 当我将相同的 EJB 注入 servlet 时,出现空指针异常(我的 EJB 为空)。
myapp.ear 包含以下内容:
- myapp.war (servlet 所在位置)
- myapp.jar (EJB 所在位置)
EJB 接口:
package com.mycompany.myapp.ejb.hello;
@Local
public interface HelloEjb {
public final static String NAME = "HelloEjb";
public String sayHello();
}
EJB Impl :
package com.mycompany.myapp.ejb.hello;
@Stateless(name = HelloEjb.NAME)
public class HelloEjbImpl implements HelloEjb {
@Override
public String sayHello() {
return "Hello";
}
}
我的 Servlet :
public class HelloServlet extends HttpServlet {
@EJB
private HelloEjb helloEjb;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("This is my stateless session-EJB: " + helloEjb.sayHello());
out.println("</html>");
out.close();
}
}
WAR 中的 web.xml 没有什么特别的,只有 servlet 定义,没有 ejb 标签:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="MyApp" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- General -->
<display-name>My App</display-name>
<!-- Standard Action Servlet Configuration -->
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.mycompany.myapp.web.servlet.HelloServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
如果在 servlet 中我使用查找,它就可以工作能够获取 EJB:
helloEjb = ctx.lookup("ejblocal:com.mycompany.myapp.ejb.hello.HelloEjb");
我正在使用 Websphere 7 & EJB 3.0
任何帮助将不胜感激。
I am trying to inject an EJB with the @EJB annotation :
- when I inject an EJB into another EJB it works fine.
- when I inject the same EJB into a servlet I got a null pointer exception (my EJB is null).
myapp.ear contains the following :
- myapp.war (where the servlet is located)
- myapp.jar (where the EJBs are)
EJB Interface :
package com.mycompany.myapp.ejb.hello;
@Local
public interface HelloEjb {
public final static String NAME = "HelloEjb";
public String sayHello();
}
EJB Impl :
package com.mycompany.myapp.ejb.hello;
@Stateless(name = HelloEjb.NAME)
public class HelloEjbImpl implements HelloEjb {
@Override
public String sayHello() {
return "Hello";
}
}
My Servlet :
public class HelloServlet extends HttpServlet {
@EJB
private HelloEjb helloEjb;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("This is my stateless session-EJB: " + helloEjb.sayHello());
out.println("</html>");
out.close();
}
}
The web.xml in the WAR has nothing spetacular, only the servlet defintion, no ejb tags :
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="MyApp" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- General -->
<display-name>My App</display-name>
<!-- Standard Action Servlet Configuration -->
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.mycompany.myapp.web.servlet.HelloServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
If in the servlet I use lookup, it works I am able to get the EJB :
helloEjb = ctx.lookup("ejblocal:com.mycompany.myapp.ejb.hello.HelloEjb");
I am using Websphere 7 & EJB 3.0
Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的第一个镜头是,如果您为无状态 EJB 指定一个“名称”,您应该使用“mappedName”属性注入它,例如。 '@EJB(mappedName="foo.FooRemote")'
My first shot is, if you specifiy a 'name' for your stateless EJB you should inject it with 'mappedName' attribute eg. '@EJB(mappedName="foo.FooRemote")'
您使用的是哪个修复包?在 FixPack 11 之前,WAS7 的依赖注入机制已经彻底崩溃。尝试升级到 15(不要使用最新的 17,与 commons-logging 一起使用的令人讨厌的错误)
Which Fixpack are you on? Prior to FixPack 11, the dependency injection mechanism of WAS7 was shot to hell. Try upgrading to 15 (don't use the latest, 17, nasty bug working with commons-logging)