@EJB注释在客户端 [英] @EJB annotation in clients

查看:113
本文介绍了@EJB注释在客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用NetBeans,我在包含 main()的类中执行以下操作,它的工作原理是:

Using NetBeans, I do the following in the class containing main(), and it works:

import javax.ejb.EJB;

public class Master {
    @EJB
    TestBeanARemote x;

    public static void main(String[] args) {
        Master m = new Master();
        m.doStuff();
    }
//doStuff includes x, but it works, so who cares.
...

但是,如果我在一个被调用的类中这样做,它将失败。看来,一个调用的类需要我避免使用注释,而是使用整个 InitialContext()安装程序。

If I do that in a called class, however, it fails. It seems that a called class requires me to avoid using annotations and instead use a the whole InitialContext() setup.

String testRun(String arg) {
   InitialContext ic;
    try {
        ic = new InitialContext();
        x = (TestBeanARemote) ic.lookup("com.bnncpa.testing.TestBeanARemote");
        return x.testRun(arg);

    }

完整的,失败的副本如下:

The full, failing copy is below:

package enterpriseapplication1;
public class Main {

    private Secondary x = new Secondary();

    public static void main(String[] args) {
        Main m = new Main();
        m.doStuff();
    }

    public void doStuff() {
        System.out.println(x.testRun("bar"));
    }

}

package enterpriseapplication1;
import org.mine.testing.TestBeanARemote;
import javax.ejb.EJB;

public class Secondary {
   @EJB
   static private TestBeanARemote x;

   String testRun(String arg) {
       return x.testRun(arg);
   }
}

有什么特别的原因,为什么 @EJB 可能不会在包的所有类中工作?我想要只要标记 @EJB ,无论我在哪里使用。

Is there a particular reason why @EJB might not work in all classes of a package? I'd like to be able to simply tag @EJB wherever I'm using one.

有没有一些更好的方法去做,我完全失踪了。

Is there some better way to go about this that I'm missing entirely?

编辑:为了解决使用appclient的担忧,这是我的堆栈跟踪:

To address the concern over using an appclient, here's my stack trace:

May 11, 2009 4:24:46 PM com.sun.enterprise.appclient.MainWithModuleSupport <init>
WARNING: ACC003: Application threw an exception.
java.lang.NullPointerException
    at enterpriseapplication1.Secondary.testRun(Secondary.java:20)
    at enterpriseapplication1.Main.doStuff(Main.java:27)
    at enterpriseapplication1.Main.main(Main.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.enterprise.util.Utility.invokeApplicationMain(Utility.java:266)
    at com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:449)
    at com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:259)
    at com.sun.enterprise.appclient.Main.main(Main.java:200)
Exception in thread "main" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:461)
    at com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:259)
    at com.sun.enterprise.appclient.Main.main(Main.java:200)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.enterprise.util.Utility.invokeApplicationMain(Utility.java:266)
    at com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:449)
    ... 2 more
Caused by: java.lang.NullPointerException
    at enterpriseapplication1.Secondary.testRun(Secondary.java:20)
    at enterpriseapplication1.Main.doStuff(Main.java:27)
    at enterpriseapplication1.Main.main(Main.java:23)
    ... 8 more
Java Result: 1


推荐答案

问题是@EJB只会被注入到管理类中。

The problem is that @EJB will only be injected in to "managed" classes.

在Java EE中,管理类很少。值得注意的应用程序客户端(在这种情况下,您的主要),EJB(无状态和有状态EJB,消息Bean等)和Servlet。

In Java EE there are very few managed classes. Notably Application Clients (your "main" here in this case), EJBs (Stateless and Stateful EJBs, Message Beans, etc.), and Servlets.

其他通用类,JPA实体等)将不会注入资源,您将需要依靠查找机制来访问您的资源。

Anything else (i.e. generic classes, JPA entities, etc.) will not have the resources injected, and you will need to rely on the lookup mechanism to get access to your resources.

这篇关于@EJB注释在客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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