java.lang.LinkageError:加载器约束违规:解析方法时

时间:2016-05-13 10:30:35

标签: java maven java-ee jboss

我有以下内容:

1。业务层中的EJB

package ejb.x.y;

@Stateless
public class FileFormatConvertor {  
    public byte[] fromExcelToCsv(Workbook workbook, Sheet sheet, String delimiter) throws Exception {
        ...
}

2。 Web层中的Bean

package web.x.y;

@Named
@SessionScoped
public class FileUploadViewAction implements Serializable {
    @EJB
    private FileFormatConvertor fileFormatConvertor ;   
    // Other declarations
    if (fileType == Type.EXCEL) {
        bytesToUpload =   fileFormatConvertor.fromExcelToCsv(workbook, sheet, delimiter);
    }
    // Rest of code
}

3。 POM文件

Ear 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>
    <parent>
        <artifactId>xy-portal</artifactId>
        <groupId>com.xy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.xy</groupId>
    <artifactId>ear-x-y</artifactId>
    <version>${project.version}</version>
    <packaging>ear</packaging>
    <name>ear-x-y</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <finalName>ear-x-y</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <version>6</version>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                    <modules>
                        <webModule>
                            <groupId>com.xy</groupId>
                            <artifactId>web-x-y</artifactId>
                            <contextRoot>/xy</contextRoot>
                            <bundleFileName>xy-portal-web.war</bundleFileName>
                        </webModule>
                        <ejbModule>
                            <groupId>com.xy</groupId>
                            <artifactId>ejb-x-y</artifactId>
                            <bundleFileName>xy-portal-ejb.jar</bundleFileName>
                        </ejbModule>
                    </modules>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jboss.as.plugins</groupId>
                <artifactId>jboss-as-maven-plugin</artifactId>
                <configuration>
                    <skip>false</skip>  
                    <hostname>0.0.0.0</hostname>
                    <!--<hostname>xy</hostname>-->
                    <port>0000</port>
                    <filename>xy-portal-ear.ear</filename>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.xy</groupId>
            <artifactId>xy-portal-ejb</artifactId>
            <version>${project.version}</version>
            <type>ejb</type>
        </dependency>   
        <dependency>
            <groupId>com.xy</groupId>
            <artifactId>xy-portal-web</artifactId>
            <version>${project.version}</version>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxb-provider</artifactId>
            <version>2.3.1.GA</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>2.3.1.GA</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>xy-client</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>xy-datamodel</artifactId>
            <version>${project.version}</version>
        </dependency>       
    </dependencies>    
</project>

Ejb 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>
    <parent>
        <artifactId>xy-portal</artifactId>
        <groupId>com.xy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.xy</groupId>
    <artifactId>xy-portal-ejb</artifactId>
    <version>${project.version}</version>
    <packaging>ejb</packaging>
    <name>xy-portal-ejb</name>
    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>          
        <dependency>
            <groupId>org.jboss.as</groupId>
            <artifactId>jboss-as-ejb3</artifactId>
            <version>7.1.2.Final</version>
            <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-validator</artifactId>
                </exclusion>
            </exclusions>
        </dependency>      
        <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-6.0</artifactId>
            <version>3.0.3.Final</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.infinispan</groupId>
            <artifactId>infinispan-core</artifactId>
            <version>6.0.2.Final</version>
        </dependency>        
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>        
        <dependency>
           <groupId>org.testng</groupId>
           <artifactId>testng</artifactId>                           
           <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.testng</groupId>
            <artifactId>arquillian-testng-container</artifactId>
            <scope>test</scope>
        </dependency>       
        <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-api</artifactId>
            <scope>test</scope>
        </dependency>       
        <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-api-maven</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-api-maven-archive</artifactId>
            <scope>test</scope>        
        </dependency>
        <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-impl-maven-archive</artifactId>
            <scope>test</scope>        
        </dependency>
        <dependency>
            <groupId>org.eu.ingwar.tools</groupId>
            <artifactId>arquillian-suite-extension</artifactId>         
            <scope>test</scope>
        </dependency>       
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>3.7</version>
            <scope>test</scope>
        </dependency>              
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <ejbVersion>3.1</ejbVersion>
                    <archive>
                        <manifestEntries>
                          <Dependencies>org.infinispan export</Dependencies>
                        </manifestEntries> 
                    </archive>
                </configuration>
            </plugin>       
        </plugins>
    </build>
</project>

Web 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>
    <parent>
        <artifactId>xy-portal</artifactId>
        <groupId>com.xy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>xy-portal-web</artifactId>
    <version>${project.version}</version>
    <packaging>war</packaging>

    <name>xy-portal-web</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>org.primefaces.themes</groupId>
            <artifactId>all-themes</artifactId>
            <version>1.0.10</version>
        </dependency>

        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>5.2</version>
        </dependency>

        <dependency>
            <groupId>org.omnifaces</groupId>
            <artifactId>omnifaces</artifactId>
            <version>1.8.1</version>
        </dependency>

        <dependency>
            <groupId>org.primefaces.extensions</groupId>
            <artifactId>primefaces-extensions</artifactId>
            <version>3.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>font-awesome</artifactId>
            <version>4.6.1</version>
        </dependency>       

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>xy-client</artifactId>
            <version>${project.version}</version>
            <scope>provided</scope>
            <exclusions>                
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-validator</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>xy-portal-ejb</artifactId>
            <version>${project.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>xy-datamodel</artifactId>
            <version>${project.version}</version>
            <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <groupId>javax.validation</groupId>
                    <artifactId>validation-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-validator</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-jpamodelgen</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxb-provider</artifactId>
            <version>2.3.1.GA</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>2.3.1.GA</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>jaxrs-api</artifactId>
            <version>3.0.8.Final</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.2.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-web-6.0</artifactId>
            <version>3.0.3.Final</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>jboss</groupId>
            <artifactId>jbosssx</artifactId>
            <version>3.2.3</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jboss</groupId>
            <artifactId>jboss-jaas</artifactId>
            <version>3.2.3</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.glxn</groupId>
            <artifactId>qrgen</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>net.sf.barcode4j</groupId>
            <artifactId>barcode4j-light</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>           
        </plugins>
    </build>
</project>

4。例外

Caused by: javax.faces.el.EvaluationException: java.lang.LinkageError: loader constraint violation: when resolving method "ejb.x.y.FileFormatConvertor.fromExcelToCsv(Lorg/apache/poi/ss/usermodel/Workbook;Lorg/apache/poi/ss/usermodel/Sheet;Ljava/lang/String;)[B" the class loader (instance of org/jboss/modules/ModuleClassLoader) of the current class, web.x.y.FileUploadViewAction, and the class loader (instance of org/jboss/modules/ModuleClassLoader) for resolved class, ejb.x.y.FileFormatConvertor, have different Class objects for the type Lorg/apache/poi/ss/usermodel/Workbook;Lorg/apache/poi/ss/usermodel/Sheet;Ljava/lang/String;)[B used in the signature
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) [jsf-impl-2.1.7-jbossorg-2.jar:]
    ... 36 more

获得此异常我做错了什么? 到目前为止,我已经做了以下事情: 1.检查循环类路径依赖性 2.将ejb作为托管bean移动到Web层(解决问题但是其他客户端在其他地方访问bean,使业务层更自然) 3.搜索此站点以查找类似的问题/解决方案路径但未找到

请帮忙......

2 个答案:

答案 0 :(得分:0)

你war和ejb.jar都有自己的类加载器,所以每个类加载器都有自己的poi类定义。

你应该尝试将poi罐子和所有常见的罐子放到ejb.jar并在ear / lib中进行战争。将依赖关系标记为pom.xml中的“提供”,用于ejb.jar和war。

这样war和ejb.jar都将继承由其父类加载器(ear classloader)定义的类。

你永远不应该尝试将类加载器中定义的类传递给另一个类加载器,而是使用在公共类加载器中定义的类。

答案 1 :(得分:0)

我让这个工作。我的回答只是强调mvera在这里说的话...... 例如。 war和ejb模块的pom.xml内容共享以下依赖项:

<dependencies>
...
    <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>         
    </dependency>
...
</dependencies>

这就是我做的......

  1. 将此依赖项条目(以及所有共享依赖项)复制到ear模块中的pom.xml。

  2. 标记ejb和war模块的pom.xml文件中提供的共享依赖项,以便pom.xml文件中的结果依赖项现在看起来类似于下面的

    <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version> 
            <scope>provided</scope>
    </dependency>
    
  3. 重建您的项目!