EJB 世界的新手... EJB 客户端中的空指针异常 [英] New to EJB world... Null pointer exception in EJB client

查看:19
本文介绍了EJB 世界的新手... EJB 客户端中的空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 EJB,我正在尝试执行 EJB In Action 书中给出的 Helloworld 示例.

I am learning EJB and I am trying to execute the Helloworld example given in EJB In Action book.

我的应用服务器是 JBoss,我在正确的目录中为 bean 类和接口创建了 Jar 文件(我可以在 JMX 控制台中看到 EJB).

My app server is JBoss, I created the Jar file for the bean class and interface in the right directory( I can see the EJB in JMX console).

现在我使用 EJB 注释创建了一个简单的客户端,但我收到了 NullPointerException.

Now I created a simple client using EJB annotations, but I am getting a NullPointerException.

这是我的客户端代码.

客户端代码:

package com.client;
import javax.ejb.EJB;
import com.EJB.*;

public class HelloWorldClient {
 @EJB
 private static HelloWorldInterface HelloBean;

 public static void main(String[] args)
 {
  HelloBean.SayHelloWorldInEJB();
 }
}

Bean 类

package com.EJB;

import javax.ejb.Stateless;

@Stateless
public class HelloWorldBean implements HelloWorldInterface {

 public void SayHelloWorldInEJB() {
  // TODO Auto-generated method stub
  System.out.println("Hello world from the world of EJB");

 }
}

界面

package com.EJB;
import javax.ejb.Local;;

@Local
public interface HelloWorldInterface {
 public void SayHelloWorldInEJB();
}

注意:我尝试使用将接口指定为远程,它仍然不起作用.

Note: I tried using specifying the interface as Remote, it still didn't work.

到目前为止我所做的步骤.1) 创建文件 EJB 文件2) 制作 build.xml 并部署 EJB.

Steps that I did so far to get to this point. 1) Created the file EJB files 2) Made the build.xml and deployed the EJB.

我是否缺少任何配置文件???

Am I missing any configuration files ???

推荐答案

现在我使用 EJB 注释创建了一个简单的客户端,但我收到了 NullPointerException.

Now I created a simple client using EJB annotations, but I am getting a NullPointerException.

您的客户端代码看起来像一个应用程序客户端 并且此类客户端应该部署在应用服务器上,然后在应用程序客户端容器 (ACC) 中执行,以便可以进行注入.启动 ACC 需要特定于应用程序服务器的命令.

Your client code looks like an Application Client and such client is supposed to be deployed on the app server and then executed in an Application Client Container (ACC) so that injection can occur. Starting the ACC requires an application server specific command.

以下wiki解释了ACC在JBoss中的使用(如何打包、部署和启动一个ACC):如何在 JBoss-5 中使用应用程序客户端.

The following wiki explains the usage of the ACC in JBoss (how to package, deploy and launch an ACC): How to use an application client in JBoss-5.

如果您不想使用应用程序客户端容器,而只是通过 java 命令运行应用程序客户端类,则无法进行注入,您必须执行 JNDI查找.

If you don't want to use an Application Client Container and instead just run the application client class through a java command, injection won't be possible and you'll have to perform a JNDI lookup.

在这两种情况下,您都必须为 bean 提供和使用远程业务接口.

And in both cases, you'll have to provide and use a remote business interface for your bean.

这篇关于EJB 世界的新手... EJB 客户端中的空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆