java EJB 注入错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11846866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:37:44  来源:igfitidea点击:

EJB Injection error

javaglassfishejbweblogicjava-ee-6

提问by Heidarzadeh

I have an EAR file (JEE6) containing one ejb-jar (sample-ejb) file and one war file (sample-web). Here is the structure:

我有一个 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)

when I want to inject TestEjb to StartupEjb or TestListener:

当我想将 TestEjb 注入 StartupEjb 或 TestListener 时:

@EJB
private TestEjb testEjb;

I get following error:

我收到以下错误:

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.

Both Glassfish and Weblogic can not find TestEjb!

Glassfish 和 Weblogic 都找不到 TestEjb!

By the way, when I create a remote interface for TestEjb and use that for injection it works well! But I don't want to define remote interfaces and I want to use EJB with no interface. I don't think that I must define a remote interface for this simple usecase!

顺便说一句,当我为 TestEjb 创建一个远程接口并将其用于注入时,它运行良好!但是我不想定义远程接口,我想使用没有接口的 EJB。我不认为我必须为这个简单的用例定义一个远程接口!

You can download the source code from here: https://dl.dropbox.com/sh/63hn3n9y3p5ypvi/SsG9iZIvx9/sample.zip?dl=1

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

It's a simple Maven project created using NetBeans.

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

回答by Martin Metz

You normaly MUST define a local interface, e. g.

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

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

If you explicitly avoid this interface you must mark your bean as @LocalBean:

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

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

PS: Code is untested but should help.

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