SpringBoot 2.6.0發(fā)布:禁止循環(huán)依賴(lài),還有哪些實(shí)用的更新?
Spring Boot 2.6.0已經(jīng)正式發(fā)布,快看看作了哪些升級(jí)。
1、禁止了循環(huán)依賴(lài)
循環(huán)依賴(lài)問(wèn)題一直困擾大家,也是面試常問(wèn)題目之一,對(duì)循環(huán)依賴(lài)不了解的可以看這篇:Spring高頻面試題:如何解決循環(huán)依賴(lài)問(wèn)題
Spring Boot 2.6.0之后,如果程序中存在循環(huán)依賴(lài)問(wèn)題,啟動(dòng)上就會(huì)失敗,報(bào)錯(cuò):
┌─────┐
|??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.
如果程序設(shè)計(jì)非常合理,完全避免了循環(huán)依賴(lài),是最完美的。如果實(shí)在不能,Spring Boot也提供了折中解決辦法,在報(bào)錯(cuò)信息中已經(jīng)明示:
As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
需要我們?cè)谂渲梦募pplication.properties里加上這個(gè)屬性:
spring.main.allow-circular-references?=?true
程序依然可以正常啟動(dòng)。
2、新增自定義脫敏規(guī)則
Spring Boot 2.6.0 支持/env端點(diǎn)和configprops配置屬性的自定義脫敏,自定義SanitizingFunction類(lèi)型的Bean即可實(shí)現(xiàn),如下:
@Bean
public?SanitizingFunction?pwdSanitizingFunction()?{
????return?data?->?{
????????org.springframework.core.env.PropertySource>?propertySource?=?data.getPropertySource();
????????String?key?=?data.getKey();
????????
????????//?僅對(duì)redis.properties里面的某些key做脫敏
????????if?(propertySource.getName().contains("redis.properties"))?{
????????????if?(key.equals("redis.pwd"))?{
????????????????return?data.withValue(SANITIZED_VALUE);
????????????}
????????}
????????return?data;
????};
}
對(duì)于部分?jǐn)?shù)據(jù)脫敏,這個(gè)改進(jìn)非常靈活,很有用。
3、Redis自動(dòng)開(kāi)啟連接池
在2.6.0之前的版本,配置Redis時(shí)是否啟用連接池是由使用者指定,2.6.0之后是默認(rèn)開(kāi)啟,如果需要關(guān)閉,則需要配置:
spring.redis.jedis.pool.enabled?=?false
或者
spring.redis.lettuce.pool.enabled?=?false?
說(shuō)明Spring Boot支持大家使用Redis連接池。
4、支持使用WebTestClient來(lái)測(cè)試Spring MVC
開(kāi)發(fā)人員可以使用 WebTestClient 在模擬環(huán)境中測(cè)試程序,只需要在Mock環(huán)境中使用?@AutoConfigureMockMvc?注釋?zhuān)涂梢暂p松注入 WebTestClient。,省去編寫(xiě)測(cè)試程序。
5、默認(rèn)使用全新匹配策略
請(qǐng)求路徑與 Spring MVC 處理映射匹配的默認(rèn)策略已從AntPathMatcher更改為PathPatternParser。你可以設(shè)置spring.mvc.pathmatch.matching-strategy為ant-path-matcher來(lái)改變它。
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字樣,但保持了很好的向下兼容性:除了不支持將**寫(xiě)在path中間之外,其它的匹配規(guī)則從行為上均保持和AntPathMatcher一致,并且還新增了強(qiáng)大的{*pathVariable}的支持。
6、增強(qiáng)了/info管理端點(diǎn),加上了Java運(yùn)行時(shí)信息
如下:
{
??"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應(yīng)用現(xiàn)在支持在Cookie中添加SameSite。 支持在主端口或管理端口上配置健康組。 在 Spring Boot 2.4 中棄用的類(lèi)、方法和屬性已在此版本中刪除。 支持 Log4j2 復(fù)合配置 支持構(gòu)建信息屬性排除
另外需要注意的是,Spring Boot每年會(huì)在5月份和11月份發(fā)布兩個(gè)中型版本,每個(gè)中型版本提供1年的免費(fèi)支持,那也就意味著2.4.x已經(jīng)停止了版本停止(免費(fèi))支持。不過(guò)對(duì)本次版本更新點(diǎn)有所了解即可,等待下次大版本更新再去學(xué),一更新馬上用新的實(shí)在學(xué)不動(dòng)~~
參考:
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.6.0-Configuration-Changelog
END
推薦閱讀 一鍵生成Springboot & Vue項(xiàng)目!【畢設(shè)神器】
Java可視化編程工具系列(一)
Java可視化編程工具系列(二)
順便給大家推薦一個(gè)GitHub項(xiàng)目,這個(gè) GitHub 整理了上千本常用技術(shù)PDF,絕大部分核心的技術(shù)書(shū)籍都可以在這里找到,
GitHub地址:https://github.com/javadevbooks/books
Gitee地址:https://gitee.com/javadevbooks/books
電子書(shū)已經(jīng)更新好了,你們需要的可以自行下載了,記得點(diǎn)一個(gè)star,持續(xù)更新中..

