Springboot项目升级2.2.x升至2.7.x的示例代码
作者:Box_clf
依赖管理
spring-boot-starter-parent 升级为2.7.1
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- 升级为2.7.x的版本-->
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>兼容老的配置文件格式,spring提供了专门的依赖进行兼容,但是建议还是在升级能成功运行之后进行配置的同步修改
<!-- 升级2.7后支持yml环境变量名兼容-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-properties-migrator</artifactId>
<scope>runtime</scope>
</dependency>如果使用了bootstrap.yml文件,升级之后默认是不会使用bootstrap.yml了,所以可能会导致启动之后配置没有生效,需要引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.4</version>
</dependency>配置修改
2.7.x不会处理循环依赖问题,需要手动配置修改application.yml添加配置 spring.main.allow-circular-references

如果有nacos的使用,版本替换为:
<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2021.0.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.0.4.0</version>
</dependency>同时需要支持yml配置,添加 spring.config.import
spring:
profiles:
active: "@profileActive@"
application:
name: saas
cloud:
nacos:
server-addr: xx.xx.xx.xx:8848
discovery:
server-addr: xx.xx.xx.xx:8848
namespace: f8f5c0c4-adc9-4c37-be6b-ddc
config:
server-addr: xx.xx.xx.xx:8848 #配置中心
file-extension: yaml #配置文件后缀名 dataId = application.name + file-extension
namespace: f8f5c0c4-adc9-4c37-be6b-ddc
config:
import:
- optional:nacos:${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} #配置中心报错问题定位思路
启动报错:
Web application could not be started as there was no org.springframework.boot.web.servlet.server.ServletWebServerFactory bean defined in the context.
最开始搜索报错,很多网上说的一般都是忘记加注解 @SpringBootApplication ,或者没有引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>但是检查并没有遗漏。开始根据问题本身进行定位。
报错本身就是没有找到ServletWebServerFactory,那么思路就是检查xxxxxxxConfiguration中的代码是否正常执行,这里就是 org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration
因为是Tomcat方式启动所以只需要关心 tomcatServletWebServerFactory 方法

这里我设置了debug的方式但是并没有走到这里
再次全局搜索怀疑可能是自动装配的类 org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration 有问题


再次尝试debug发现依然没有走到,继续思考查看类上方相关的Conditional注解条件是否满足,猜测可能是 @ConditionalOnWebApplication(type = Type.SERVLET) 不满足
点进注解查看

继续查看对应的 org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition ,搜索刚才的相关的 SERVLET ,继续debug发现type为null直接返回了

继续查找调用的地方进行debug发现 autoConfigurationMetadata 传入的就有问题,很多value都是空字符串

继续定位到上级调用,发现传入的数据依然和上面一样存在问题

继续跟踪定位,终于调到了我们自己写的代码了

继续查看上级调用,发现这个类是我们自己拷贝出来的Springboot内部的类,做了一些自定义的改造

这里就说明,可能是从低版本的Springboot中拷贝出来的,但是升级到2.7.x之后可能存在对应的逻辑变化,所以需要对比一下,大概是哪些代码不一致

Ctrl+A全选Ctrl+C复制我们自己写的代码,然后跳进内部的 AutoConfigurationImportSelector 再进行比较


这里发现我们自己的代码少了一行
ImportCandidates.load(AutoConfiguration.class, getBeanClassLoader()).forEach(configurations::add);
根据断言可以知道对应的这段代码不仅是读取了 META-INF/spring.factories 中的自动装配信息,还读取了 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 中的自动装配的bean

最后查看了包结构,确实如此,升级后 META-INF/spring.factories 没有了 ServletWebServerFactoryAutoConfiguration 而是放到了 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 中。最后解决就是复制对应的代码到自定义的类中,支持 imports 中的自动装配
到此这篇关于Springboot项目升级2.2.x升至2.7.x的示例代码的文章就介绍到这了,更多相关Springboot 升级2.2.x升至2.7.x内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
