1

我遇到了麻烦,我一直在开发我的项目。我开始开发一个基于 Spring 框架的爱好项目,一段时间后,项目的各个部分开始融合在一起。主要有两个模块,一个是EJB模块,一个是WEB模块。存储库和核心 EJB bean 存储在 EJB 模块中,并在 WEB 模块中使用。由于我以前使用过 elasticsearch,并且对数据处理的速度和效率印象深刻,所以我想我要把它集成到我的爱好项目中。

为了集成elasticsearch,我采取了适当的措施并将所需的库添加到项目的pom文件中。乍一看,似乎一切都很顺利,但后来事实证明并非如此。我为 elasticsearch 创建了存储库,对它们进行了测试,我很高兴,当我测试另一个之前完美运行的功能时,我受到了以下例外的欢迎:

11:41:25,213 错误 [io.undertow.request](默认任务 33)UT005023:对 /axyz-web/test/test 的异常处理请求:org.springframework.web.util.NestedServletException:处理程序调度失败;嵌套异常是 java.lang.LinkageError: 加载程序约束违规:解析接口方法时“com.myproject.axyz.beans.stateless.user.UserManagementLocal.findUsers(Lorg/springframework/data/domain/Pageable;)Lorg/springframework/data /域/页;" 当前类 com/myproject/axyz/spring/controllers/test/TestController 的类加载器(org/jboss/modules/ModuleClassLoader 的实例)和类加载器(org/jboss/modules/ModuleClassLoader 的实例)方法的定义类,com/myproject/axyz/beans/stateless/user/UserManagementLocal,

第一次,我以为我在 war 和 ejb 项目中编译了不同版本的 spring-boot-data-elasticsearch(因为我也想在我的 web 项目中使用 crud 存储库进行弹性搜索),经过一些研究,我发现我的怀疑得到了证实.

首先,我从WEB项目中删除了依赖,因为它依赖于EJB模块,然后再次编译项目然后部署它。所以spring-boot-data-elasticsearch被编译成EJB模块。

我收到的下一条消息如下:

11:54:08,407 错误 [org.springframework.web.context.ContextLoader](ServerService 线程池 -- 382)上下文初始化失败:org.springframework.beans.factory.BeanDefinitionStoreException:从 ServletContext 资源解析 XML 文档时出现意外异常 [/WEBINF /applicationContext.xml]; 嵌套异常是 org.springframework.beans.FatalBeanException: Class [org.springframework.data.elasticsearch.config.ElasticsearchNamespaceHandler] for namespace [ http://www.springframework.org/schema/data/elasticsearch]没有实现 [org .springframework.beans.factory.xml.NamespaceHandler] 接口。

好的,下一轮,我将 spring-boot-data-elasticsearch 编译到 EAR 模块中,并将两个模块中的库的范围设置为提供。

像这样 :

耳朵模块:

        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        <version>${spring.boot.framework.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.xyz.xy</groupId>
        <artifactId>xyz-ejb</artifactId>
        <version>1.0-SNAPSHOT</version>
        <type>ejb</type>
    </dependency>
    <dependency>
        <groupId>com.xyz.xy</groupId>
        <artifactId>xyz-web</artifactId>
        <version>1.0-SNAPSHOT</version>
        <type>war</type>
    </dependency>
</dependencies>

<build>
    <resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/test/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <excludes>
                <exclude>**/*local.properties</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <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>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <version>6</version>
                <defaultLibBundleDir>lib</defaultLibBundleDir>
                <!------- 
                FINAL DESPAIR
                <modules>
                    <jarModule>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
                        <bundleFileName>spring-boot-starter-elasticsearch.jar</bundleFileName>
                    </jarModule>
                </modules> 

                --------------------------->
            </configuration>
        </plugin>
    </plugins>
</build>

EJB 模块:

    <dependencies>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.2</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
        <version>2.5.2</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.2</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>commons-jxpath</groupId>
        <artifactId>commons-jxpath</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.10</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>3.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- For JBOSS -->
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.10</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.5.4</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>aopalliance</groupId>
        <artifactId>aopalliance</artifactId>
        <version>1.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
        <scope>compile</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${spring.version}</version>
        <scope>compile</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.version}</version>
        <scope>compile</scope>
    </dependency>

    <!--  SPRING BOOT -->

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>${spring.boot.framework.version}</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-commons</artifactId>
            </exclusion>
        </exclusions>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        <version>${spring.boot.framework.version}</version>
        <scope>provided</scope>
    </dependency>


    <!-- dependency>
        JUST ANOTHER TRYING.......
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-elasticsearch</artifactId>
        <version>2.1.4.RELEASE</version>
    </dependency -->

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring.boot.framework.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
        <scope>compile</scope>
    </dependency>   


    <!-- END  -->
</dependencies>


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.arquillian</groupId>
            <artifactId>arquillian-bom</artifactId>
            <version>1.1.11.Final</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>


<build>
    <resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/test/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <excludes>
                <exclude>**/*local.properties</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring.boot.framework.version}</version>
        </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>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ejb-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <ejbVersion>3.1</ejbVersion>
                <addClasspath>true</addClasspath>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>7.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

网络模块:

     .... Spring dependencies ......
    <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    ............

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        <version>${spring.boot.framework.version}</version>
        <scope>provided</scope>
    </dependency>

    <!--
        EJB MODULE 
    -->
    <dependency>
        <groupId>MY EJB PROJECT</groupId>
        <artifactId>MY EJB PROJECT-ejb</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <build>
    <resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/test/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <excludes>
                <exclude>**/*local.properties</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <skipTests>${test.skip}</skipTests>
            </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>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.1</version>
            <executions>
                <execution>
                    <phase>${shade.phase}</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <minimizeJar>true</minimizeJar>
                <artifactSet>
                    <includes>
                        <include>com.google.guava:guava</include>
                        <include>net.sf.trove4j:trove4j</include>
                        <include>org.mvel:mvel2</include>
                        <include>com.fasterxml.jackson.core:jackson-core</include>
                        <include>com.fasterxml.jackson.dataformat:jackson-dataformat-smile</include>
                        <include>com.fasterxml.jackson.dataformat:jackson-dataformat-yaml</include>
                        <include>joda-time:joda-time</include>
                        <include>io.netty:netty</include>
                        <include>com.ning:compress-lzf</include>
                    </includes>
                </artifactSet>
                <relocations>
                    <relocation>
                        <pattern>com.google.common</pattern>
                        <shadedPattern>org.elasticsearch.common</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>gnu.trove</pattern>
                        <shadedPattern>org.elasticsearch.common.trove</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>jsr166y</pattern>
                        <shadedPattern>org.elasticsearch.common.util.concurrent.jsr166y</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>jsr166e</pattern>
                        <shadedPattern>org.elasticsearch.common.util.concurrent.jsr166e</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>org.mvel2</pattern>
                        <shadedPattern>org.elasticsearch.common.mvel2</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>com.fasterxml.jackson</pattern>
                        <shadedPattern>org.elasticsearch.common.jackson</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>org.joda</pattern>
                        <shadedPattern>org.elasticsearch.common.joda</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>org.jboss.netty</pattern>
                        <shadedPattern>org.elasticsearch.common.netty</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>com.ning.compress</pattern>
                        <shadedPattern>org.elasticsearch.common.compress</shadedPattern>
                    </relocation>
                </relocations>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/license/**</exclude>
                            <exclude>META-INF/*</exclude>
                            <exclude>META-INF/maven/**</exclude>
                            <exclude>LICENSE</exclude>
                            <exclude>NOTICE</exclude>
                            <exclude>/*.txt</exclude>
                            <exclude>build.properties</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>7.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

版本号

    <properties>
     <spring.boot.framework.version>1.5.5.RELEASE</spring.boot.framework.version>
     <spring.version>4.3.8.RELEASE</spring.version>
     <org.springframework.version>4.3.8.RELEASE</org.springframework.version>
</properties>

示例连接:

    <elasticsearch:repositories base-package="com.xxxxxxxxxxxxxxxxxxx.spring.elasticsearch.repositories"/>  

<elasticsearch:transport-client id="esClient" cluster-nodes="localhost:9300" />

<bean id="esConnection" class="com.xxxxxxxxxxxxxx.core.elasticsearch.ESConnection" init-method="init"  scope="session">
    <constructor-arg name="con" ref="container"></constructor-arg>
    <property name="client" ref="esClient"></property>
</bean>

<bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
    <constructor-arg name="client" ref="esClient"/>
</bean>

我也试过排除spring-data-commons(因为那个库里面定义了Pageable和Page等组件),单独编译进WEB模块,但是效果一样,我想用elasticsearch和它的两个模块中的存储库分开。

总之,如果我不将elasticsearch库编译到我的WEB模块中,它将永远无法解释XML或创建bean(我也尝试了JAVA注释,尝试了更新版本,情况是一样的),所以不知道怎么解决这个问题,因为库编译成WEB模块会出现链接错误。

有谁知道如何解决这个问题?有可能吗?

4

0 回答 0