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



我目前有一个WAR项目和一个EJB项目,以及一个将它们捆绑在一起的EAR项目。

实际上,它正是Netbeans示例中的项目Maven>Enterprise Application

然后,我在WAR项目中创建一个web服务,并希望它在我的EAR项目中调用会话bean。很简单。

我的代码看起来像这样:

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

    @EJB
    NewSessionBean bean;

起初,我的编译器抱怨找不到这个NewSessionBean类,这很公平,因为它们在不同的项目中。

通过Netbeans的提示,我看到Netbeans建议我添加EJB项目作为依赖项,所以我这样做了

我的Maven XML现在在WAR POM:中有这个

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

太棒了!

但是现在,当我右键单击EAR文件并单击Run(又名Deploy)时,它显示Web服务找不到EJB引用:(

为什么部署EAR不尊重WAR对EJB项目的依赖性?即使我的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)

就使用EJB代理而言,我建议更改依赖项的类型。另外,因为您正在部署.ear归档,我希望您已经有一个将EBJ添加到ear中的jar。你可以通过浏览.ear来检查它。如果你发现带有EJB的jar已经添加到.ear档案中,你可能需要将scope设置为provided,这样它就不会被添加两次。

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

最新更新