Spring Boot与Spring Cloud版本兼容问题:如何解决方法不存在错误?
Spring Boot与Spring Cloud版本兼容问题解决
问题背景
当前使用Spring Boot 3.3.1开发,pom.xml依赖配置如下:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.9.1</version> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-impl</artifactId> <version>0.11.2</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>1.4.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies>
错误详情
运行时触发方法不存在异常:
- 调用位置:
org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:161)
- 不存在的方法:
'void org.springframework.boot.builder.SpringApplicationBuilder.<init>(java.lang.Object[])'
- 调用类加载路径:
jar:file:/C:/Users/Pradeep/.m2/repository/org/springframework/cloud/spring-cloud-context/1.3.6.RELEASE/spring-cloud-context-1.3.6.RELEASE.jar!/org/springframework/cloud/bootstrap/BootstrapApplicationListener.class
- 被调用类加载路径:
jar:file:/C:/Users/Pradeep/.m2/repository/org/springframework/boot/spring-boot/3.3.1/spring-boot-3.3.1.jar!/org/springframework/boot/builder/SpringApplicationBuilder.class
问题原因
当前使用的spring-cloud-starter-hystrix:1.4.7.RELEASE属于Spring Cloud Edgware版本,该版本仅兼容Spring Boot 1.5.x;而你使用的Spring Boot 3.3.1基于Spring 6构建,两者API差异极大,旧版Spring Cloud依赖的SpringApplicationBuilder构造方法在Spring Boot 3.x中已被移除,导致方法找不到错误。
解决方案:版本匹配
Spring Boot 3.x需搭配**Spring Cloud 2022.0.x(代号Kilburn)或2023.0.x(代号Leyton)**版本,这两个版本完全适配Spring Boot 3.x:
推荐版本组合
- Spring Boot 3.3.1 + Spring Cloud 2023.0.3(Leyton SR3)
- 最新稳定兼容组合,支持Spring Cloud组件的最新特性
- Spring Boot 3.3.1 + Spring Cloud 2022.0.5(Kilburn SR5)
- 适合需要长期稳定支持的场景
pom.xml修改要点
- 移除旧版Hystrix依赖(Hystrix已停止维护,Spring Cloud 3.x推荐使用Resilience4j替代)
- 更新
spring-cloud-dependencies版本:
<properties> <spring-cloud.version>2023.0.3</spring-cloud.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
- 如需断路器功能,添加Resilience4j依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId> </dependency>
额外兼容性调整
- 旧版
jjwt:0.9.1与Spring Boot 3.x存在兼容性问题,建议升级至最新稳定版:
<dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-api</artifactId> <version>0.12.5</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-impl</artifactId> <version>0.12.5</version> <scope>runtime</scope> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-jackson</artifactId> <version>0.12.5</version> <scope>runtime</scope> </dependency>
javax.xml.bind:jaxb-api替换为Jakarta EE版本适配Spring Boot 3.x:
<dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <scope>runtime</scope> </dependency>
内容的提问来源于stack exchange,提问作者GUJJARLAPUDI PRADEEP
相关产品推荐
相关产品推荐

