【问题标题】:Conflict problem building the Spring boot application构建 Spring Boot 应用程序的冲突问题
【发布时间】:2021-09-26 13:37:16
【问题描述】:

我在构建 Spring Boot 应用程序时遇到问题。我们需要使用 maven 构建具有“lib/bin/conf”结构的项目。我用另一个项目做了,没有问题。但是现在发生了冲突,建议采取行动。


Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.springframework.context.support.GenericApplicationContext.setApplicationStartup(GenericApplicationContext.java:165)

The following method did not exist:

    'void org.springframework.beans.factory.support.DefaultListableBeanFactory.setApplicationStartup(org.springframework.core.metrics.ApplicationStartup)'

The method's class, org.springframework.beans.factory.support.DefaultListableBeanFactory, is available from the following locations:

    jar:file:/target/OrderManager/libs/communication-latest.jar!/org/springframework/beans/factory/support/DefaultListableBeanFactory.class
    jar:file:target/OrderManager/libs/spring-beans-5.3.8.jar!/org/springframework/beans/factory/support/DefaultListableBeanFactory.class

The class hierarchy was loaded from the following locations:

    org.springframework.beans.factory.support.DefaultListableBeanFactory: file:target/OrderManager/libs/communication-latest.jar
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory: file:target/OrderManager/libs/communication-latest.jar
    org.springframework.beans.factory.support.AbstractBeanFactory: file:target/OrderManager/libs/communication-latest.jar
    org.springframework.beans.factory.support.FactoryBeanRegistrySupport: file:target/OrderManager/libs/communication-latest.jar
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry: file:target/OrderManager/libs/communication-latest.jar
    org.springframework.core.SimpleAliasRegistry: file:target/OrderManager/libs/communication-latest.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.beans.factory.support.DefaultListableBeanFactory

我该如何解决这个问题?我正在使用我们公司提供的名为通信的库。 这是我的 pom 文件。

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.artifact</groupId>
    <artifactId>ordermanager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Order Manager Component</name>
    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>Hoxton.SR7</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.4.2</version>
        </dependency>


        <dependency>
            <groupId>com.excample.myArtifact</groupId>
            <artifactId>communication</artifactId>
            <version>latest</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>appassembler-maven-plugin</artifactId>
                <version>2.1.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>assemble</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <assembleDirectory>target/OrderManager</assembleDirectory>
                    <repositoryName>libs</repositoryName>

                    <configurationDirectory>conf</configurationDirectory>
                    <configurationSourceDirectory>src/main/resources</configurationSourceDirectory>
                    <copyConfigurationDirectory>true</copyConfigurationDirectory>
                    <repositoryLayout>flat</repositoryLayout>
                    <useWildcardClassPath>true</useWildcardClassPath>

                    <programs>
                        <program>
                            <mainClass>com.example.artifact.ordermanager.OrderManagerComponentApplication</mainClass>
                            <id>OrderManager</id>
                        </program>
                    </programs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

  • 您正在混合 spring boot 的版本。在您的父母中,您使用的是 2.5.2 版本,但在您使用 2.4.2 的执行器的依赖项中......只有在您知道自己在做什么的情况下,才不要定义自己的组件版本。此外,您为什么将 appassembler-maven-plugin 用于 Spring Boot 应用程序?请改用 spring-boot-maven-plugin....
  • 谢谢,我需要像我所说的 bin - conf - lib 结构而不是胖 jar 文件(包含所有依赖项)的构建。那么,能否请您给我一个教程来配置和构建用于指定结构的项目?
  • 我的问题是:为什么不使用默认值?我建议通过默认值构建您的 Spring Boot 应用程序,然后可能使用 appassembler...但我不确定...
  • 谢谢,我的朋友。根据您的观点,我从与 spring 相关的包中删除了版本标签。问题仍然存在。我也卸下了执行器,但问题仍然存在。
  • 谢谢,查到通讯库的版本是2.3.3.RELEASE。我把项目的spring boot版本设置为2.3.3.RELEASE,问题就解决了。

标签: spring spring-boot maven build


【解决方案1】:

为了解决这个冲突,您可以使用 excludes-dependencies https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html

在这种情况下,应该是

<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>

来自

<groupId>com.excample.myArtifact</groupId>
<artifactId>communication</artifactId>

【讨论】:

  • 谢谢,我按照下面的方法做了,但问题仍然存在。 com.excample.myArtifactcommunicationlatestorg.springframework spring-beans
【解决方案2】:

答案已经有了:

更正应用程序的类路径,使其包含一个兼容的 org.springframework.beans.factory.support.DefaultListableBeanFactory 版本

您的问题是,您有 2 个 JAR 文件,其中包含相同的类 org.springframework.beans.factory.support.DefaultListableBeanFactory。它们是:

  • /target/OrderManager/libs/communication-latest.jar
  • target/OrderManager/libs/spring-beans-5.3.8.jar

您必须删除其中一个。现在我不知道你的项目和架构,但是如果你使用的是公司定制的spring库,那么你应该删除标准的spring jar。为此使用maven的exclude mechanism,它是一种所谓的瞬态依赖,所以你没有明确定义它,但是你定义的依赖之一就是依赖它。

首先,您必须找出哪个依赖项带来了 spring-beans 依赖项。使用 maven 的依赖关系树来做到这一点:

mvn dependency:tree

您也可以使用dependency analysis 或阅读整个dependency management

如果你想退出communication-latest.jar,你应该在你的 pom.xml 中删除以下内容:

<dependency>
  <groupId>com.excample.myArtifact</groupId> 
  <artifactId>communication</artifactId>
  <version>latest</version>
</dependency>

【讨论】:

  • 谢谢我的朋友,我不想删除公司库,所以,我尝试按照@Alexy Semenyuk 告诉我的那样从我们的库中排除弹簧豆,但问题仍然存在。我也尝试了你的方法,我检查了依赖树,发现org.springframework:spring-web:jar:5.3.8:compile 正在使用这种体面。我试图从这个包中排除spring bean,但问题仍然存在。
  • 首先做一个 mvn clean ,因为你总是在你的目标文件夹中进行组装,而旧的 JAR 可能仍然挂在那里......试试吧!
  • 我这样做了,问题没有解决。我做了mvn clean 然后mvn install。我打开目标文件夹并单击bin/ordermanager.bat。问题没有解决,错误正在显示。
【解决方案3】:

问题在于版本与通信库中使用的 spring boot 不匹配,而不是主项目的 spring boot。于是,我把spring boot的版本改成了2.3.3.RELEASE,问题就解决了。
有朋友告诉我排除spring-beans神器。我没有这样做,版本更改就足够了,但是测试有关此主题的方法可能很有用。

【讨论】:

    猜你喜欢
    • 2019-07-24
    • 2021-05-03
    • 2021-01-07
    • 1970-01-01
    • 2019-07-25
    • 2020-10-19
    • 2020-07-13
    • 2023-01-23
    • 2022-01-07
    相关资源
    最近更新 更多