简体   繁体   English

如何在Maven中的WAR项目中指定EJB引用?

[英]How do I specify EJB reference in WAR project in Maven?

I currently have a WAR project and an EJB project, as well as an EAR project which bundles them together. 我目前有一个WAR项目和一个EJB项目,以及一个将它们捆绑在一起的EAR项目。

Actually it is exactly the project Maven > Enterprise Application in the samples for Netbeans. 实际上,正是Netbeans样本中的Maven> Enterprise Application项目。

I then create a web service in the WAR project, and want it to call a session bean in my EAR project. 然后我在WAR项目中创建一个Web服务,并希望它在我的EAR项目中调用会话bean。 Simple enough. 很简单。

My code looks something like this : 我的代码看起来像这样:

@WebService(serviceName = "NewWebService")
public class NewWebService {


    @EJB
    NewSessionBean bean;

At first my compiler complains that it can't find this NewSessionBean class which is fair enough since they are in different projects. 起初我的编译器抱怨它找不到这个公平的NewSessionBean类,因为它们在不同的项目中。

Through Netbeans 'hints' I see that Netbeans is suggesting to me to add the EJB project as a dependency, so I do. 通过Netbeans的提示,我看到Netbeans建议我将EJB项目添加为依赖项,所以我这样做。

My Maven XML now has this in the WAR POM : 我的Maven XML现在在WAR POM中有这个:

<dependency>
    <groupId>com.mycompany</groupId>
    <artifactId>mavenEJBExample-ejb</artifactId>
    <version>1.0-SNAPSHOT</version>
    <type>jar</type>
</dependency>

Great! 大!

But now when I right click the EAR file and click on Run (aka Deploy), it says that the Web Service can't find the EJB reference :( 但是现在当我右键单击EAR文件并单击Run(又称Deploy)时,它表示Web Service无法找到EJB引用:(

Why does deploying the EAR not respect the dependency of the WAR on the EJB projcect? 为什么部署EAR不尊重WAR对EJB项目的依赖性? Even when I have the dependency in my Maven XML? 即使我在Maven XML中具有依赖性?

Schwerwiegend:   Ausnahme beim Deployment der Anwendung [mavenEJBExample-ear]
Schwerwiegend:   Exception during lifecycle processing
java.lang.IllegalArgumentException: Referenz [Remote ejb-ref name=com.mycompany.NewWebService/bean,Remote 3.x interface =com.mycompany.NewSessionBean,ejb-link=null,lookup=,mappedName=,jndi-name=,refType=Session] kann nicht aufgelöst werden, da [2]-EJBs in der Anwendung mit Schnittstelle com.mycompany.NewSessionBean vorhanden sind. \nEinige der möglichen Gründe: \n1. Die EJB-Bean-Klasse wurde in einer EAR-Library verpackt (oder durch ein anderes Library-Verfahren, das die Library für alle Komponentenmodule sichtbar macht). Dadurch schließen alle Komponentenmodule diese Bean-Klasse indirekt ein. \n2. Die EJB-Bean-Klasse wurde in einem Komponentenmodul verpackt, das das EJB entweder direkt oder indirekt über Manifest, WEB-INF/lib referenziert. \nDie EJB-Bean-Klasse darf nur im deklarierenden EJB-Modul und nicht in den referenzierenden Modulen verpackt werden. Die referenzierenden Module dürfen nur EJB-Schnittstellen einschließen.
    at com.sun.enterprise.deployment.util.ComponentValidator.accept(ComponentValidator.java:356)
    at com.sun.enterprise.deployment.util.DefaultDOLVisitor.accept(DefaultDOLVisitor.java:78)
    at com.sun.enterprise.deployment.util.ComponentValidator.accept(ComponentValidator.java:123)
    at com.sun.enterprise.deployment.util.ApplicationValidator.accept(ApplicationValidator.java:147)

As far as you are going to use EJB proxy, I would suggest to change the type of the dependency. 至于你要使用EJB代理,我建议改变依赖的类型。 Also because you are deploying an .ear archive I would expect you already have a jar with EBJs added to ear. 另外因为你正在部署一个.ear存档,我希望你已经有一个添加了EBJ的jar。 You can check it by exploring your .ear. 您可以通过浏览.ear来查看它。 If you find the jar with your EJBs already added to the .ear archive, you may want to set scope to provided so it were not added twice. 如果您发现已将EJB添加到.ear存档中的jar,则可能需要将范围设置为提供,以使其不会添加两次。

<dependency>
    <groupId>com.mycompany</groupId>
    <artifactId>mavenEJBExample-ejb</artifactId>
    <type>ejb-client</type>
    <scope>provided</scope>
    <version>1.0-SNAPSHOT</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM