javascript
SpringBoot 2.6.0发布:禁止循环依赖,还有哪些实用的更新?
今日推薦
Spring Boot 2.6.0已經正式發布,快看看作了哪些升級。
1、禁止了循環依賴
循環依賴問題一直困擾大家,也是面試常問題目之一
Spring Boot 2.6.0之后,如果程序中存在循環依賴問題,啟動上就會失敗,報錯:
┌─────┐ |??a?(field?private?com.zhiyin.TestB?com.zhiyin.TestA.b) ↑?????↓ |??b?(field?private?com.zhiyin.TestA?com.zhiyin.TestB.a) └─────┘Action:Relying?upon?circular?references?is?discouraged?and?they?are?prohibited?by?default.?Update?your?application?to?remove?the?dependency?cycle?between?beans.?As?a?last?resort,?it?may?be?possible?to?break?the?cycle?automatically?by?setting?spring.main.allow-circular-references?to?true.如果程序設計非常合理,完全避免了循環依賴,是最完美的。如果實在不能,Spring Boot也提供了折中解決辦法,在報錯信息中已經明示:
As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
需要我們在配置文件application.properties里加上這個屬性:
spring.main.allow-circular-references?=?true程序依然可以正常啟動。
2、新增自定義脫敏規則
Spring Boot 2.6.0 支持/env端點和configprops配置屬性的自定義脫敏,自定義SanitizingFunction類型的Bean即可實現,如下:
@Bean public?SanitizingFunction?pwdSanitizingFunction()?{return?data?->?{org.springframework.core.env.PropertySource<?>?propertySource?=?data.getPropertySource();String?key?=?data.getKey();//?僅對redis.properties里面的某些key做脫敏if?(propertySource.getName().contains("redis.properties"))?{if?(key.equals("redis.pwd"))?{return?data.withValue(SANITIZED_VALUE);}}return?data;}; }對于部分數據脫敏,這個改進非常靈活,很有用。
3、Redis自動開啟連接池
在2.6.0之前的版本,配置Redis時是否啟用連接池是由使用者指定,2.6.0之后是默認開啟,如果需要關閉,則需要配置:
spring.redis.jedis.pool.enabled?=?false或者
spring.redis.lettuce.pool.enabled?=?false說明Spring Boot支持大家使用Redis連接池。
4、支持使用WebTestClient來測試Spring MVC
開發人員可以使用 WebTestClient 在模擬環境中測試程序,只需要在Mock環境中使用 @AutoConfigureMockMvc 注釋,就可以輕松注入 WebTestClient。,省去編寫測試程序。
5、默認使用全新匹配策略
請求路徑與 Spring MVC 處理映射匹配的默認策略已從AntPathMatcher更改為PathPatternParser。你可以設置spring.mvc.pathmatch.matching-strategy為ant-path-matcher來改變它。
2.6.0之前:
public?static?class?Pathmatch?{private?MatchingStrategy?matchingStrategy?=?MatchingStrategy.ANT_PATH_MATCHER; }2.6.0之后:
public?static?class?Pathmatch?{private?MatchingStrategy?matchingStrategy?=?MatchingStrategy.PATH_PATTERN_PARSER; }兩者差異上:PathPattern去掉了Ant字樣,但保持了很好的向下兼容性:除了不支持將**寫在path中間之外,其它的匹配規則從行為上均保持和AntPathMatcher一致,并且還新增了強大的{*pathVariable}的支持。
6、增強了/info管理端點,加上了Java運行時信息
如下:
{"java":?{"vendor":?"BellSoft","version":?"17","runtime":?{"name":?"OpenJDK?Runtime?Environment","version":?"17+35-LTS"},"jvm":?{"name":?"OpenJDK?64-Bit?Server?VM","vendor":?"BellSoft","version":?"17+35-LTS"}} }7、其他變化
Servlet應用現在支持在Cookie中添加SameSite。
支持在主端口或管理端口上配置健康組。
在 Spring Boot 2.4 中棄用的類、方法和屬性已在此版本中刪除。
支持 Log4j2 復合配置
支持構建信息屬性排除
另外需要注意的是,Spring Boot每年會在5月份和11月份發布兩個中型版本,每個中型版本提供1年的免費支持,那也就意味著2.4.x已經停止了版本停止(免費)支持。不過對本次版本更新點有所了解即可,等待下次大版本更新再去學,一更新馬上用新的實在學不動~~
參考:
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.6.0-Configuration-Changelog
推薦文章1、一款高顏值的 SpringBoot+JPA 博客項目2、超優 Vue+Element+Spring 中后端解決方案3、推薦幾個支付項目!4、推薦一個 Java 企業信息化系統5、一款基于 Spring Boot 的現代化社區(論壇/問答/社交網絡/博客)總結
以上是生活随笔為你收集整理的SpringBoot 2.6.0发布:禁止循环依赖,还有哪些实用的更新?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 扔掉工具类,Mybatis一个简单配置搞
- 下一篇: Docker + Intellij ID