将 Spring boot 1.5 迁移到 2.x 无法启动应用程序

Posted

技术标签:

【中文标题】将 Spring boot 1.5 迁移到 2.x 无法启动应用程序【英文标题】:Migrating Spring boot 1.5 to 2.x unable to start app 【发布时间】:2019-02-01 21:22:22 【问题描述】:

我是 Spring Boot 新手,正在尝试将现有应用程序从 1.5 迁移到 2.0。 我浏览了迁移指南,发现在我的父 pom 中包含 preoperties-migrator 作为依赖项将指出要进行的更改以进行迁移。因此我在 pom.xml 中添加了以下依赖项强>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-properties-migrator</artifactId>
    <version>2.0.4.RELEASE</version>
</dependency>

一旦我添加了以下依赖项,应用程序启动失败并出现以下错误。

"message":"应用程序启动 失败","logger_name":"org.springframework.boot.SpringApplication","thread_name":"main","level":"ERROR","level_value":40000,"stack_trace":"java.lang.NoClassDefFoundError: org/springframework/boot/env/OriginTrackedMapPropertySource\n\tat org.springframework.boot.context.properties.migrator.PropertiesMigrationListener.onApplicationPreparedEvent(PropertiesMigrationListener.java:67)\n\tat org.springframework.boot.context.properties.migrator.PropertiesMigrationListener.onApplicationEvent(PropertiesMigrationListener.java:57)\n\tat org.springframework.boot.context.properties.migrator.PropertiesMigrationListener.onApplicationEvent(PropertiesMigrationListener.java:44)\n\tat org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)\n\tat org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)\n\tat org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)\n\tat org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)\n\tat org.springframework.boot.context.event.EventPublishingRunListener.contextLoaded(EventPublishingRunListener.java:91)\n\tat org.springframework.boot.SpringApplicationRunListeners.contextLoaded(SpringApplicationRunListeners.java:66)\n\tat org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:356)\n\tat org.springframework.boot.SpringApplication.run(SpringApplication.java:301)\n\tat org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)\n\tat org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)\n\tat com.move.aws.eai.inventory.common.app.BaseApplication.run(BaseApplication.java:140)\n\tat com.move.aws.eai.inventory.bulk.Application.main(Application.java:10)\n导致 作者:java.lang.ClassNotFoundException: org.springframework.boot.env.OriginTrackedMapPropertySource\n\tat java.net.URLClassLoader.findClass(URLClassLoader.java:381)\n\tat java.lang.ClassLoader.loadClass(ClassLoader.java:424)\n\tat sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)\n\tat java.lang.ClassLoader.loadClass(ClassLoader.java:357)\n\t... 15 常见 省略帧\n"

谁能指出问题所在。

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>
    <groupId>com.move.aws.eai.inventory</groupId>
    <artifactId>aurora</artifactId>
    <version>1.0.20-SNAPSHOT</version>
  </parent>
  <groupId>com.move.aws.eai.inventory.aurora</groupId>
  <artifactId>bulk</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <name>Inventory and Waitlist Bulk API</name>
  <dependencies>
    <dependency>
      <groupId>com.move.aws.eai.inventory.aurora</groupId>
      <artifactId>common</artifactId>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-sts</artifactId>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-s3</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-dbcp2</artifactId>
    </dependency>
    <!-- Features toggling -->
    <dependency>
      <groupId>org.togglz</groupId>
      <artifactId>togglz-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.togglz</groupId>
      <artifactId>togglz-console</artifactId>
    </dependency>
    <!-- Testing -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
    </dependency>
    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-module-junit4</artifactId>
    </dependency>
    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-api-mockito2</artifactId>
    </dependency>
    <dependency>
      <groupId>com.github.stefanbirkner</groupId>
      <artifactId>system-rules</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter-test</artifactId>
    </dependency>
  </dependencies>
  <build>
    <finalName>inventory-bulk</finalName>
    <plugins>
      <plugin>
        <!-- Package as an executable jar -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
              <goal>build-info</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <executable>true</executable>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>EnterpriseSystems-inventory-maven</name>
      <url>https://artifactory.moveaws.com/artifactory/EnterpriseSystems-inventory-maven</url>
    </repository>
    <repository>
      <snapshots />
      <id>snapshots</id>
      <name>EnterpriseSystems-inventory-maven</name>
      <url>https://artifactory.moveaws.com/artifactory/EnterpriseSystems-inventory-maven</url>
    </repository>
  </repositories>
</project>

【问题讨论】:

在我看来,您的某些 Spring 依赖项的版本不兼容。我怀疑您没有使用 Spring 的 BOM,或者您的父级中有一些明确的版本。 如果我发布我的父 pom.xml,你能指出什么问题吗? @BoristheSpider 由于错误出现在org.springframework.boot.context.properties 中,我猜这与您对Togglz 的使用有关。从 the documentation 看来,Boot 2.x 需要 2.6.1.Final。您使用的版本是否正确? 你能添加你的父 pom 定义吗? com.move.aws.eai.inventoryaurora1.0.20-SNAPSHOT 【参考方案1】:

这是一个愚蠢的错误。

我将父 pom.xml 更改为使用最新的 Spring boot 版本。

【讨论】:

我无法升级到最新的 Spring Boot,因为我们与不同的微服务共享会话,而这些微服务正在使用 Spring Boot 1.5.9 @KrishLakshmanan 不确定,但您的父 pom.xml 中的更改是否也反映在您的微服务 pom 中?

以上是关于将 Spring boot 1.5 迁移到 2.x 无法启动应用程序的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 2.x 的性能问题?

spring boot 1.x.x 到 spring boot 2.x.x 的那些变化

Spring Boot 2.0干货系列:Spring Boot1.5X升级到2.0指南

将 flyway 版本 1.5 升级到 3.2

从 Spring Boot 1.5 升级时为 Spring Boot 2.0 acuator 框架配置安全性

升级到 Spring Boot 2 后,如何向 prometheus 公开缓存指标?