Servlet 中的 EJB 注入失败

发布于 2024-10-07 03:52:10 字数 1749 浏览 0 评论 0原文

我试图将 EJB 无状态 bean 注入到 servlet 中,但 servlet 抛出 NullPointerExcetion。我正在使用 JBOSS 来部署 EJB 和 servlet。

我对 Java 世界比较陌生,所以我发布了我遵循的步骤。

接口

package MavenEJB.Bidding`
import javax.ejb.Local;

@Local
public interface PlaceBid {
 public String AddBid();
}

Bean

package MavenEJB.Bidding;
import javax.ejb.Stateless;
@Stateless(name="PlaceBid")
public class PlaceBidBean implements PlaceBid {
 public PlaceBidBean(){}

 /**
  * Include logic to add the bid 
  */

 public String AddBid(){
  return "Placed bid using EJB"; 
 }
}

我使用maven创建了bean的jar文件,并将该jar文件复制到JBOSS的“deploy”目录中。我可以在 JMX 控制台中看到部署的 bean。

JMX 控制台中的全局 JNDI 命名空间

+- PlaceBid (class: org.jnp.interfaces.NamingContext)
  |   +- local (proxy: $Proxy63 implements interface MavenEJB.Bidding.PlaceBid,interface org.jboss.ejb3.JBossProxy)

我的 servlet 代码

public class PlaceBidServlet extends HttpServlet {

    @EJB
    private PlaceBid placeBid;

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  response.setContentType("text/html");

  PrintWriter out = response.getWriter();
     out.println("<HTML>");
     out.println("<HEAD><TITLE>Hello</TITLE></HEAD>");
     out.println("<BODY>");
  out.println("Output from EJB" +placeBid.AddBid());

     //out.println("Output from EJB" );
     out.println("</BODY></HTML>");  
 }

....
}

当我指向 servlet 的 URL 时,我收到 NullPointerException。当我评论 bean 并打印其他内容时,它打印得很好。所以我确信问题出在 servlet 中的 EJB 依赖注入上。 我尝试了其他地方建议的许多解决方案,但没有任何效果,请有人帮助我。

I am trying to inject an EJB stateless bean in to a servlet, but the servlet throws a NullPointerExcetion. I am using JBOSS to deploy the EJB and servlet.

I am relatively new to the Java world, so I am posting the steps I followed.

Interface

package MavenEJB.Bidding`
import javax.ejb.Local;

@Local
public interface PlaceBid {
 public String AddBid();
}

Bean

package MavenEJB.Bidding;
import javax.ejb.Stateless;
@Stateless(name="PlaceBid")
public class PlaceBidBean implements PlaceBid {
 public PlaceBidBean(){}

 /**
  * Include logic to add the bid 
  */

 public String AddBid(){
  return "Placed bid using EJB"; 
 }
}

I created a jar file of the bean using maven and I copied the jar file to "deploy" directory of JBOSS. I am able to see the bean deployed in the JMX console.

Global JNDI Namespace in JMX console

+- PlaceBid (class: org.jnp.interfaces.NamingContext)
  |   +- local (proxy: $Proxy63 implements interface MavenEJB.Bidding.PlaceBid,interface org.jboss.ejb3.JBossProxy)

My servlet code

public class PlaceBidServlet extends HttpServlet {

    @EJB
    private PlaceBid placeBid;

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  response.setContentType("text/html");

  PrintWriter out = response.getWriter();
     out.println("<HTML>");
     out.println("<HEAD><TITLE>Hello</TITLE></HEAD>");
     out.println("<BODY>");
  out.println("Output from EJB" +placeBid.AddBid());

     //out.println("Output from EJB" );
     out.println("</BODY></HTML>");  
 }

....
}

When I point to the URL of my servlet, I get NullPointerException. When I comment the bean and print something else, it prints fine. So I am sure the problem is with the EJB Dependency Injection in the servlet.
I tried many solutions suggested else where, nothing really worked, someone please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂归处 2024-10-14 03:52:10

经过几个小时后,我发现了问题,我将其发布,以便其他人可以受益。
问题是,我使用的是 JBOSS 4.2.3,正如这篇文章 http://community.jboss.org/ message/410211 表明 jboss 4.2.x 不支持 servlet 中的 EJB 注入。

我使用的是Jboss 5.1,而且如果servlet和EJB不在一个ear包中,则必须使用mappedName来进行EJB注入。我将 Servlet 放在 WAR 中,将 EJB 放在单独的 Jar 中。查看这篇文章了解更多详细信息 http://community.jboss.org/message/8196#8196

After a lot of hours I found the problem, I am posting it so someone else can benefit.
The problem is, I was using JBOSS 4.2.3 and as this post http://community.jboss.org/message/410211 suggests jboss 4.2.x does not support EJB injection in servlets.

I used Jboss 5.1, also if both the servlet and EJB are not in a single ear package, one must use mappedName to the EJB Injection. I had the servlets in a WAR and the EJB in a separate Jar. Check this post for more details http://community.jboss.org/message/8196#8196

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文