EJB注入错误

5

我有一个EAR文件(JEE6),其中包含一个ejb-jar(sample-ejb)文件和一个war文件(sample-web)。 以下是结构:

sample-ear
         |----sample-ejb.jar
         |         |---TestEjb.java (@Singleton)
         |
         |----sample-web
                   |---StartupEjb.java (@Singleton,@Startup)
                   |---TestListener.java (@WebListener)

当我想将TestEjb注入到StartupEjb或TestListener中时:
@EJB
private TestEjb testEjb;

我遇到了以下错误:

Glassfish:

Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=com.sample.StartupEjb/testEjb,Remote 3.x interface =com.sample.TestEjb,ejb-link=null,lookup=,mappedName=,jndi-name=com.sample.TestEjb,refType=Session into class com.sample.StartupEjb: Lookup failed for 'java:comp/env/com.sample.StartupEjb/testEjb' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}

Weblogic:

Caused By: weblogic.application.naming.ReferenceResolutionException: [J2EE:160200]Error resolving ejb-ref "com.sample.StartupEjb/testEjb" from module "sample-web-1.0-SNAPSHOT.war" of application "com.sample_sample-ear_ear_1.0-SNAPSHOT". The ejb-ref does not have an ejb-link and the JNDI name of the target bean has not been specified. Attempts to automatically link the ejb-ref to its target bean failed because no EJBs in the application were found to implement the "com.sample.TestEjb" interface. Link or map this ejb-ref to its target EJB and ensure the interfaces declared in the ejb-ref are correct.

Glassfish和Weblogic都无法找到TestEjb!

顺便提一下,当我为TestEjb创建一个远程接口并将其用于注入时,它可以正常工作!但是我不想定义远程接口,我想使用没有接口的EJB。 我认为对于这个简单的用例,我不必定义远程接口!

您可以从这里下载源代码:https://dl.dropbox.com/sh/63hn3n9y3p5ypvi/SsG9iZIvx9/sample.zip?dl=1

这是使用NetBeans创建的简单Maven项目。


我使用了@LocalBean并且它工作正常。因为我正在使用JEE6,所以我不再需要这个注释,但如果没有它,它就无法工作。很奇怪吧! - Mehdi
也许有点奇怪,但是“无视图”在不同的部署“实体”之间不存在,因此您需要某种视图。 - esej
1个回答

6

通常情况下,您必须定义一个本地接口,例如:

@Singleton
@Local(PublicInterface.class)
public class MyBean implements PublicInterface {
  ...
}

如果您明确避免使用此接口,则必须将您的bean标记为@LocalBean:

@Singleton
@LocalBean
public class MyBean {
  ...
}

PS:代码未经测试,但应该会有所帮助。


JEE6设置了一个默认的隐式本地接口,您不需要真正声明一个。 - Bill Rosmus
感谢BillR。@LocalBean解决方案可行,但据我所知,在JEE6中我们不需要使用@LocalBean。我正在使用Weblogic 12.1.1和JEE6项目。 - Mehdi
是的,你说得对,JEE6的默认设置很好用(只要确保你只有一个接口)。 - Martin Metz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接