2014-03-02 2 views
0

Java EE Maven 프로젝트에서 작업 중이며 Glassfish4 서버에 배포하려고 할 때 종속성 주입에 문제가 있습니다.Java EE, Maven 프로젝트 - Glassfish4의 CDI 배포 오류

받는다는 프로젝트는과 같이 최고 수준의 치어 아래 세 가지 별도의 모듈로 구성되어 있습니다 :

크로노스 : ​​

  • EJB

크로노스입니다 부모 모듈, 웹은 프론트 엔드 코드 (JSF, ManagedBeans)를 처리하고, ejb는 백엔드 코드 (EJB, JPA)를 가지며, 귀는 인공물 생성 및 배치를 처리합니다. 이 게시물의 끝에는 실제 pom.xml 파일이 나열되어 있습니다.

ejb 모듈 안에 StaffDAO 인터페이스와 StaffDAOImpl 상태 비어있는 bean (@ javax.ejb.Stateless 주석이 있음)이 있습니다. 현재이 코드에서는 유일한 EJB bean이며 StaffDAO를 구현하는 유일한 bean입니다.

글래스 피시에 배포하려고 할 때 우리는 오류를 다음 얻을 StaffDAO 타입의 필드에 @Inject 주석을 사용하여 웹 구성 요소의 ManagedBean은 (@ManagedBean, @RequestScoped) 내부의 빈을 주입하려고 할 때 :

대신 @EJB 주석을 사용하여
[glassfish 4.0] [SEVERE] [] [javax.enterprise.system.core] [tid: _ThreadID=35 _ThreadName=admin-listener(4)] [timeMillis: 1393775922183] [levelValue: 1000] [[ 
     Exception while loading the app : CDI deployment failure:WELD-001409 Ambiguous dependencies for type [StaffDAO] with qualifiers [@Default] at injection point [[BackedAnnotatedField] @Inject private managedbeans.DummyUserBean.staffDAO]. Possible dependencies [[Session bean [class persistence.dao.impl.StaffDAOImpl with qualifiers [@Any @Default]; local interfaces are [StaffDAO], Session bean [class persistence.dao.impl.StaffDAOImpl with qualifiers [@Any @Default]; local interfaces are [StaffDAO]]] 
    org.jboss.weld.exceptions.DeploymentException: WELD-001409 Ambiguous dependencies for type [StaffDAO] with qualifiers [@Default] at injection point [[BackedAnnotatedField] @Inject private managedbeans.DummyUserBean.staffDAO]. Possible dependencies [[Session bean [class persistence.dao.impl.StaffDAOImpl with qualifiers [@Any @Default]; local interfaces are [StaffDAO], Session bean [class persistence.dao.impl.StaffDAOImpl with qualifiers [@Any @Default]; local interfaces are [StaffDAO]]] 

우리를 준다 : 나는 확실하지 않다하지만

[glassfish 4.0] [SEVERE] [NCLS-CORE-00026] [javax.enterprise.system.core] [tid: _ThreadID=34 _ThreadName=admin-listener(3)] [timeMillis: 1393776686187] [levelValue: 1000] [[ 
    Exception during lifecycle processing 
java.lang.IllegalArgumentException: Cannot resolve reference [Remote ejb-ref name=managedbeans.DummyUserBean/staffDAO,Remote 3.x interface =persistence.dao.StaffDAO,ejb-link=null,lookup=,mappedName=,jndi-name=,refType=Session] because there are [2] ejbs in the application with interface persistence.dao.StaffDAO. 
Some of the possible causes: 
1. The EJB bean class was packaged in an ear lib library (or through any other library mechanism which makes the library visible to all component modules), this makes all the component modules include this bean class indirectly. 
2. The EJB bean class was packaged in a component module which references the EJB, either directly or indirectly through Manifest, WEB-INF/lib. 
The EJB bean class should only be packaged in the declaring ejb module and not the referencing modules. The referencing modules should only include EJB interfaces. 

나는이 두 번 포장되는 StaffDAOImpl EJB 결과 우리의 유물 세대의 실수로 인한 것으로, 생각, 하지만 나는 sufficien이 아니다. 확인하고 수정하기 위해 maven에 익숙하다. 어떤 도움을 주시면 감사하겠습니다.

코드 :

StaffDAO :

public interface StaffDAO {...} 

StaffDAOImpl :

import javax.ejb.Stateless; 

@Stateless 
public class StaffDAOImpl implements persistence.dao.StaffDAO {...} 

DummyUserBean :

import javax.faces.bean.ManagedBean; 
import javax.enterprise.context.RequestScoped; 
import javax.inject.Inject; 

@ManagedBean(name = "dummyUserBean") 
@RequestScoped 
public class DummyUserBean { 

    @Inject 
    private StaffDAO staffDAO; 

    ... 
} 

pom.xml 파일 파일은 다음과 같습니다

크로노스 : ​​

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>kronos</groupId> 
    <artifactId>kronos</artifactId> 
    <packaging>pom</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <modules> 
     <module>ejb</module> 
     <module>web</module> 
     <module>ear</module> 
    </modules> 

    <dependencies> 
     <dependency> 
      <groupId>javax</groupId> 
      <artifactId>javaee-api</artifactId> 
      <version>7.0</version> 
     </dependency> 
    </dependencies> 

    <properties> 
     <maven.compiler.source>1.7</maven.compiler.source> 
     <maven.compiler.target>1.7</maven.compiler.target> 
    </properties> 

    <build> 
     <finalName>HelloGlassfish4</finalName> 
    </build> 
</project> 

귀 :

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://maven.apache.org/POM/4.0.0" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <parent> 
     <artifactId>kronos</artifactId> 
     <groupId>kronos</groupId> 
     <version>1.0-SNAPSHOT</version> 
    </parent> 
    <modelVersion>4.0.0</modelVersion> 

    <artifactId>ear</artifactId> 
    <packaging>ear</packaging> 

    <dependencies> 
     <dependency> 
      <groupId>kronos</groupId> 
      <artifactId>ejb</artifactId> 
      <version>1.0-SNAPSHOT</version> 
      <type>ejb</type> 
     </dependency> 

     <dependency> 
      <groupId>kronos</groupId> 
      <artifactId>web</artifactId> 
      <version>1.0-SNAPSHOT</version> 
      <type>war</type> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.glassfish.maven.plugin</groupId> 
       <artifactId>maven-glassfish-plugin</artifactId> 
       <version>2.1</version> 
       <configuration> 
        <glassfishDirectory>${local.glassfish.home}</glassfishDirectory> 
        <user>${local.glassfish.user}</user> 
        <passwordFile>${local.glassfish.passfile}</passwordFile> 
        <domain> 
         <name>${local.glassfish.domain}</name> 
         <adminPort>${local.glassfish.adminPort}</adminPort> 
         <httpPort>${local.glassfish.httpPort}</httpPort> 
        </domain> 
        <components> 
         <component> 
          <name>${project.artifactId}</name> 
          <artifact>target/${project.build.finalName}.ear</artifact> 
         </component> 
        </components> 
        <debug>true</debug> 
        <terse>false</terse> 
        <echo>true</echo> 
       </configuration> 
      </plugin> 

      <plugin> 
       <artifactId>maven-ear-plugin</artifactId> 
       <version>2.8</version> 
       <configuration> 
        <modules> 
         <webModule> 
          <groupId>kronos</groupId> 
          <artifactId>web</artifactId> 
         </webModule> 
         <ejbModule> 
          <groupId>kronos</groupId> 
          <artifactId>ejb</artifactId> 
         </ejbModule> 
        </modules> 
        <defaultLibBundleDir>lib</defaultLibBundleDir> 
       </configuration> 
      </plugin> 
     </plugins> 
     <finalName>kronos</finalName> 
    </build> 
</project> 

EJB :

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <parent> 
     <artifactId>kronos</artifactId> 
     <groupId>kronos</groupId> 
     <version>1.0-SNAPSHOT</version> 
    </parent> 
    <modelVersion>4.0.0</modelVersion> 

    <artifactId>ejb</artifactId> 

    <dependencies> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>3.6.7.Final</version> 
     </dependency> 

     <dependency> 
      <groupId>org.postgresql</groupId> 
      <artifactId>postgresql</artifactId> 
      <version>9.2-1003-jdbc4</version> 
     </dependency> 

     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>1.7.6</version> 
     </dependency> 

     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.11</version> 
     </dependency> 

     <dependency> 
      <groupId>org.mockito</groupId> 
      <artifactId>mockito-all</artifactId> 
      <version>1.9.5</version> 
     </dependency> 

     <dependency> 
      <groupId>com.google.guava</groupId> 
      <artifactId>guava</artifactId> 
      <version>16.0.1</version> 
     </dependency> 

     <dependency> 
      <groupId>javax.validation</groupId> 
      <artifactId>validation-api</artifactId> 
      <version>1.0.0.GA</version> 
     </dependency> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-validator</artifactId> 
      <version>4.1.0.Final</version> 
     </dependency> 

     <dependency> 
      <groupId>joda-time</groupId> 
      <artifactId>joda-time</artifactId> 
      <version>2.3</version> 
     </dependency> 


    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.16</version> 
       <configuration> 
        <skipTests>true</skipTests> 
       </configuration> 
      </plugin> 

      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-ejb-plugin</artifactId> 
       <version>2.3</version> 
       <configuration> 
        <ejbVersion>3.2</ejbVersion> 
        <archive> 
         <manifest> 
          <addClasspath>true</addClasspath> 
         </manifest> 
        </archive> 
       </configuration> 
      </plugin> 

      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.1</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

웹 :

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <parent> 
     <artifactId>kronos</artifactId> 
     <groupId>kronos</groupId> 
     <version>1.0-SNAPSHOT</version> 
    </parent> 
    <modelVersion>4.0.0</modelVersion> 

    <artifactId>web</artifactId> 
    <packaging>war</packaging> 

    <dependencies> 
     <dependency> 
      <groupId>kronos</groupId> 
      <artifactId>ejb</artifactId> 
      <version>1.0-SNAPSHOT</version> 
     </dependency> 

     <dependency> 
      <groupId>org.primefaces</groupId> 
      <artifactId>primefaces</artifactId> 
      <version>4.0</version> 
      <type>jar</type> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <configuration> 
        <webXml>src\main\webapp\WEB-INF\web.xml</webXml> 
       </configuration> 
      </plugin> 
     </plugins> 
     <finalName>web</finalName> 
    </build> 
</project> 
+0

지금 Glassfish admin 웹 인터페이스의 모듈 및 구성 요소 뷰를보고 있는데 StaffDAOImpl이 두 번 포함되어 있습니다. 일단 ejb-1.0-SNAPSHOT.jar에 들어가면 web-1.0-SNAPSHOT.war에 한번 있습니다. 귀와 웹 모듈이 모두 ejb에 의존하기 때문에 그 중 하나를 제거하면 빌드가 중단됩니다. ejb 및 웹 모듈 (인터페이스, 데이터 모델 및 예외 클래스)에 대한 공통 코드를 유지하는 세 번째 구성 요소를 생성해야합니까, 아니면 다른 솔루션을 권장합니까? – Dalamar42

+0

제공된대로 EJB를 사용해 볼 수 있습니다. 그러면 원격을 통해 EJB를 검색합니다. 나는이 모든 것이 EAR 내부에 배치되도록 고안되었으며, 모든 EJB 참조는 해당 EAR 내에 있다고 가정합니다. –

+0

맞습니다. 나는 실제로 그것을 알아낼 수 있었다. 문제는 내가 의심했던 것이었다. 웹 모듈이 ejb에 의존한다는 사실은 ejb 클래스가 ear 패키지에 두 번 추가되었음을 의미합니다. 한 번은 ejb 항아리에, 한 번은 전쟁에서. 나는이 글을 아래에 게시하고 다른 사람들에게도 도움이 될 수 있도록 표시 할 것입니다. – Dalamar42

답변

2

그것은 내 원래 의심이 정확 밝혀졌습니다. 웹 모듈이 ejb 모듈에 의존하기 때문에 ejb 클래스가 war 아카이브에 추가되었습니다.그 때문에 귀와 전쟁에서 지어 졌을 때 Ejb 항아리는 두 번 포장되어 결국 CDI를 혼란스럽게 만들었습니다.

ejb 모듈과 웹 모듈 모두에 필요한 코드를 보유하고이 둘에 대한 종속성을 추가하고 ejb에 대한 웹 종속성을 깨뜨리는 방법으로 네 번째 maven 모듈을 수정했습니다. 이제 제대로 작동합니다.

+0

웹 모듈에 내 EJB 종속성을 항상 표시하기 위해 을 제공합니다. 실제로 나는 그것을 보지 못했지만. – Brian