有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java EJB注入错误

我有一个EAR文件(JEE6),其中包含一个EJBJAR(示例ejb)文件和一个war文件(示例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;

我发现以下错误:

玻璃鱼:

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项目


共 (1) 个答案

  1. # 1 楼答案

    通常必须定义一个本地接口,例如

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

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

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

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