gpt4 book ai didi

java - 客户端中的@EJB 注释

转载 作者:搜寻专家 更新时间:2023-10-30 21:28:01 24 4
gpt4 key购买 nike

使用 NetBeans,我在包含 main() 的类中执行以下操作,它起作用了:

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() 设置。

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

}

完整的失败副本如下:

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

是否有更好的方法来解决这个我完全想念的问题?


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

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 只会被注入(inject)“托管”类。

在 Java EE 中,托管类非常少。特别是应用程序客户端(在本例中是您的“主”)、EJB(无状态和有状态 EJB、消息 Bean 等)和 Servlet。

任何其他东西(即通用类、JPA 实体等)都不会注入(inject)资源,您将需要依赖查找机制来访问您的资源。

关于java - 客户端中的@EJB 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/848675/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com