Springboot項(xiàng)目升級(jí)2.2.x升至2.7.x的示例代碼
依賴管理
spring-boot-starter-parent 升級(jí)為2.7.1
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <!-- 升級(jí)為2.7.x的版本--> <version>2.7.1</version> <relativePath/> <!-- lookup parent from repository --> </parent>
兼容老的配置文件格式,spring提供了專門的依賴進(jìn)行兼容,但是建議還是在升級(jí)能成功運(yùn)行之后進(jìn)行配置的同步修改
<!-- 升級(jí)2.7后支持yml環(huán)境變量名兼容--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-properties-migrator</artifactId> <scope>runtime</scope> </dependency>
如果使用了bootstrap.yml文件,升級(jí)之后默認(rèn)是不會(huì)使用bootstrap.yml了,所以可能會(huì)導(dǎo)致啟動(dòng)之后配置沒有生效,需要引入依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> <version>3.0.4</version> </dependency>
配置修改
2.7.x不會(huì)處理循環(huán)依賴問題,需要手動(dòng)配置修改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>
同時(shí)需要支持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} #配置中心
報(bào)錯(cuò)問題定位思路
啟動(dòng)報(bào)錯(cuò):
Web application could not be started as there was no org.springframework.boot.web.servlet.server.ServletWebServerFactory bean defined in the context.
最開始搜索報(bào)錯(cuò),很多網(wǎng)上說的一般都是忘記加注解 @SpringBootApplication
,或者沒有引入依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
但是檢查并沒有遺漏。開始根據(jù)問題本身進(jìn)行定位。
報(bào)錯(cuò)本身就是沒有找到ServletWebServerFactory,那么思路就是檢查xxxxxxxConfiguration中的代碼是否正常執(zhí)行,這里就是 org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration
因?yàn)槭荰omcat方式啟動(dòng)所以只需要關(guān)心 tomcatServletWebServerFactory
方法
這里我設(shè)置了debug的方式但是并沒有走到這里
再次全局搜索懷疑可能是自動(dòng)裝配的類 org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration
有問題
再次嘗試debug發(fā)現(xiàn)依然沒有走到,繼續(xù)思考查看類上方相關(guān)的Conditional注解條件是否滿足,猜測(cè)可能是 @ConditionalOnWebApplication(type = Type.SERVLET)
不滿足
點(diǎn)進(jìn)注解查看
繼續(xù)查看對(duì)應(yīng)的 org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition
,搜索剛才的相關(guān)的 SERVLET
,繼續(xù)debug發(fā)現(xiàn)type為null直接返回了
繼續(xù)查找調(diào)用的地方進(jìn)行debug發(fā)現(xiàn) autoConfigurationMetadata
傳入的就有問題,很多value都是空字符串
繼續(xù)定位到上級(jí)調(diào)用,發(fā)現(xiàn)傳入的數(shù)據(jù)依然和上面一樣存在問題
繼續(xù)跟蹤定位,終于調(diào)到了我們自己寫的代碼了
繼續(xù)查看上級(jí)調(diào)用,發(fā)現(xiàn)這個(gè)類是我們自己拷貝出來的Springboot內(nèi)部的類,做了一些自定義的改造
這里就說明,可能是從低版本的Springboot中拷貝出來的,但是升級(jí)到2.7.x之后可能存在對(duì)應(yīng)的邏輯變化,所以需要對(duì)比一下,大概是哪些代碼不一致
Ctrl+A全選Ctrl+C復(fù)制我們自己寫的代碼,然后跳進(jìn)內(nèi)部的 AutoConfigurationImportSelector
再進(jìn)行比較
這里發(fā)現(xiàn)我們自己的代碼少了一行
ImportCandidates.load(AutoConfiguration.class, getBeanClassLoader()).forEach(configurations::add);
根據(jù)斷言可以知道對(duì)應(yīng)的這段代碼不僅是讀取了 META-INF/spring.factories
中的自動(dòng)裝配信息,還讀取了 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
中的自動(dòng)裝配的bean
最后查看了包結(jié)構(gòu),確實(shí)如此,升級(jí)后 META-INF/spring.factories
沒有了 ServletWebServerFactoryAutoConfiguration
而是放到了 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
中。最后解決就是復(fù)制對(duì)應(yīng)的代碼到自定義的類中,支持 imports
中的自動(dòng)裝配
到此這篇關(guān)于Springboot項(xiàng)目升級(jí)2.2.x升至2.7.x的示例代碼的文章就介紹到這了,更多相關(guān)Springboot 升級(jí)2.2.x升至2.7.x內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis批量插入更新xml方式和注解方式的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Mybatis批量插入更新xml方式和注解方式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Java實(shí)現(xiàn)的簡(jiǎn)單圖片上傳功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)的簡(jiǎn)單圖片上傳功能,結(jié)合實(shí)例形式分析了java圖片傳輸相關(guān)的檢驗(yàn)、傳輸、接收等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09SpringBoot整合RabbitMQ 手動(dòng)應(yīng)答(簡(jiǎn)單demo)
這篇文章主要介紹了SpringBoot整合RabbitMQ 手動(dòng)應(yīng)答 簡(jiǎn)單demo,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01如何使用Idea中的 Deployment 實(shí)現(xiàn)打包自動(dòng)部署
這篇文章主要介紹了使用Idea中的 Deployment 實(shí)現(xiàn)打包自動(dòng)部署,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08用Java實(shí)現(xiàn)一個(gè)靜態(tài)鏈表的方法步驟
這篇文章主要介紹了用Java實(shí)現(xiàn)一個(gè)靜態(tài)鏈表的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Java Session驗(yàn)證碼案例代碼實(shí)例解析
這篇文章主要介紹了Java Session驗(yàn)證碼案例代碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06