Spring Boot 2.4 版本升級說明

Spring Boot 2.4 版本升級說明

這是官方的Spring Boot 2.4 版本升級說明平挑,本文將通過本人以及文心一言的回答作出個人理解的解釋。對于配置屬性變更或者不常用組件的變更,過于繁瑣粒竖,我就跳過了述么。

JUnit 5’s Vintage Engine Removed from spring-boot-starter-test

If you upgrade to Spring Boot 2.4 and see test compilation errors for JUnit classes such as org.junit.Test, this may be because JUnit 5’s vintage engine has been removed from spring-boot-starter-test. The vintage engine allows tests written with JUnit 4 to be run by JUnit 5. If you do not want to migrate your tests to JUnit 5 and wish to continue using JUnit 4, add a dependency on the Vintage Engine, as shown in the following example for Maven:

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

If you are using Gradle, the equivalent configuration is shown in the following example:

testImplementation("org.junit.vintage:junit-vintage-engine") {
    exclude group: "org.hamcrest", module: "hamcrest-core"
}

Spring Boot 2.4開始,junit 5 正式移除了對junit 4的兼容依賴活孩,如果還想使用 junit 4的方法速蕊,需要手動引入兼容依賴嫂丙。

Config File Processing (application properties and YAML files)

Spring Boot 2.4 has changed the way that application.properties and application.yml files are processed. If you only have a simple application.properties or application.yml file, your upgrade should be seamless. If, however, you’ve have a more complex setup (with profile specific properties, or profile activation properties) you may need to make some changes if you want to use the new features.
If you just want Spring Boot 2.3 compatible logic, you can set a spring.config.use-legacy-processing property to true in your application.properties or application.yml file.

1.配置屬性 spring.profiles 過期,替換為 spring.config.activate.on-profile(給子配置文件命名用)规哲。
2.配置屬性 spring.profiles.include 雖然沒有過期跟啤,但是已經(jīng)不能正常使用,需要替換為 spring.profiles.group唉锌。
3.在單個配置文件內(nèi)隅肥,配置參數(shù)是按在配置文件中定義的先后順序進(jìn)行加載的,后激活加載的參數(shù)會覆蓋前面的袄简。
4.jar外部化配置優(yōu)先于內(nèi)部的配置參數(shù)腥放。
參考文章Spring Boot 2.4.0 發(fā)布,配置文件重大調(diào)整绿语,不要亂升級W狡!

Default Servlet Registration

Spring Boot 2.4 will no longer register the DefaultServlet provided by your servlet container. In most applications, it isn’t used since the Spring MVC’s DispatcherServlet is the only servlet that’s required.
You can set server.servlet.register-default-servlet to true if you find you still need the default servlet.

DefaultServlet的作用就是兜底(攔截/)汞舱,當(dāng)別的servlet都沒匹配上時就交給它來處理伍纫,一般用于處理靜態(tài)資源如.jpg,.html,.js這類的靜態(tài)文件。
但是Spring Boot注冊的DispatcherServlet的path也是/(覆蓋掉了DefaultServelt)昂芜。
現(xiàn)在的Spring Boot服務(wù)大都是REST服務(wù)莹规,并無靜態(tài)資源需要提供,因此就沒有必要啟用DefaultServletHttpRequestHandler和注冊DefaultServlet來增加不必要的開銷嘍泌神。
參考文章Spring Boot 2.4.0正式發(fā)布良漱,全新的配置文件加載機(jī)制(不向下兼容)

HTTP traces no longer include cookie headers by default

Cookie request headers and Set-Cookie response headers are no longer included in HTTP traces by default. To restore Spring Boot 2.3’s behaviour, set management.trace.http.include to cookies, errors, request-headers, response-headers.

Http Trace 是跟蹤記錄請求信息的功能,用于在actuator中顯示最近http請求信息欢际。

Removal of Plugin Management for Flatten Maven Plugin

Spring Boot’s build no longer makes use of the Flatten Maven Plugin (flatten-maven-plugin) and plugin management for it has been removed. If you were relying on Spring Boot’s managed version, you should add your own plugin management.

flatten-maven-plugin是一個解決項目版本號管理問題的插件母市。當(dāng)使用占位符統(tǒng)一版本號管理時,maven無法識別占位符损趋,就可以加入此插件患久。參考文章Maven 版本管理與 flatten-maven-plugin 插件的使用及分析

Version management for exec-maven-plugin

The version management for the exec-maven-plugin has been removed. If you are using this plugin, make sure to specify a version in your own pluginManagement.

exec-maven-plugin能夠幫助我們在Maven項目構(gòu)建過程中執(zhí)行外部操作浑槽。參考文章exec-maven-plugin的使用詳解蒋失。

Spring Framework 5.3

Spring Boot 2.4 uses Spring Framework 5.3. The Spring Framework wiki has a what’s new section with details of the new release.

spring版本升級到5.3。

Java 15 Support

Spring Boot 2.4 now fully supports (and is tested against) Java 15. The minimum supported version remains Java 8.

java支持版本最高15桐玻,最低8篙挽。

Custom property name support

When using constructor binding the name of the property is derived from the parameter name. This can be a problem if you want to use a java reserved keyword. For such situations, you can now use the @Name annotation, something like:

@ConfigurationProperties(prefix = "sample")
@ConstructorBinding
public class SampleConfigurationProperties {

  private final String importValue;

  public SampleConfigurationProperties(@Name("import") String importValue) {
    this.importValue = importValue;
  }

}

The sample above exposes a sample.import property.

@ConstructorBinding用于在構(gòu)造函數(shù)中注入配置屬性,@Name用于給別名字段注入配置屬性镊靴。

Layered jar enabled by default

This release enables layered jars and include the layertools by default. This should improve the efficiency of generated image using the build pack out-of-the-box and lets you benefit of that feature when crafting custom Dockerfile.

分層JAR是一種將應(yīng)用程序的依賴項和代碼分成不同層的技術(shù)铣卡,這樣可以使得在構(gòu)建Docker鏡像時只重新構(gòu)建有變化的層链韭,而不是每次都重新構(gòu)建整個鏡像。這可以大大提高Docker鏡像的構(gòu)建速度和效率煮落。
layertools是一個用于創(chuàng)建和管理分層JAR的工具敞峭。它可以幫助你創(chuàng)建和管理應(yīng)用程序的不同層,并確保在構(gòu)建Docker鏡像時只重新構(gòu)建有變化的層州邢。
總的來說,Spring Boot 2.4的這個更新使得構(gòu)建和部署Docker鏡像更加高效褪子,并且使得自定義Dockerfile更加容易量淌。參考文章Spring Boot官方推薦的Docker鏡像編譯方式-分層jar包

Docker/Buildpack Support
Publishing Images

The Maven plugin spring-boot:build-image goal and Gradle plugin bootBuildImage task now have the ability to publish the generated image to a Docker registry. See the Maven and Gradle plugin documentation for more details on configuring the plugins for publishing images.

通過此插件可以將生成的鏡像直接上傳到指定的docker倉庫嫌褪。

Redis Cache Metrics

If you’re using Redis caching you can now expose cache statistics via Micrometer. Metrics logged include the number puts, gets and deletes as well as hits/misses. The number of pending requests and the lock wait duration are also recorded.
To enable the feature, set spring.cache.redis.enable-statistics to true.

spring.cache.redis.enable-statistics=true 開啟此配置后呀枢,可以在一些監(jiān)控工具上看到redis緩存的命中、未命中次數(shù)等統(tǒng)計信息笼痛,看著挺有用的裙秋。

Register @WebListeners in a way that allows them to register servlets and filters

Servlet @WebListener classes are now registered in such a way that they may themselves register servlets and filters.
Earlier versions of Spring Boot registered them using a call to javax.servlet.Registration.Dynamic. This meant that the following section of Servlet specification (4.4) applied:
If the ServletContext passed to the ServletContextListener’s contextInitialized method where the ServletContextListener was neither declared in web.xml or web-fragment.xml nor annotated with @WebListener then an UnsupportedOperationException MUST be thrown for all the methods defined in ServletContext for programmatic configuration of servlets, filters and listeners.
As of Spring Boot 2.4, we no longer use dynamic registration and so it’s safe to call event.getServletContext().addServlet(…) and event.getServletContext.addFilter(…) from a ServletContextListener.contextInitialized method.
A side-effect of this change is that the Servlet container now creates the instance of the WebListener and, therefore, dependency injection such as with @Autowired can no longer be used. In such cases, @Component should be used instead.

從Spring Boot 2.4開始,Servlet容器不再直接管理@WebListener注解的類缨伊。而是由Spring容器來管理這些類的實例摘刑,并通過Servlet容器的API進(jìn)行注冊。因此刻坊,在@WebListener類中不能再使用Servlet容器的依賴注入功能枷恕,如@Autowired等谭胚。如果需要使用Spring的依賴注入功能胡控,可以將該類標(biāo)記為@Component锡搜,并將其放在Spring容器中管理纷宇。參考文章Spring Boot @ServletComponentScan 掃描 @WebServlet、@WebFilter(過濾器)拓春、@WebListener(過濾器)

推薦文章

Spring Boot 2.4.0正式發(fā)布,全新的配置文件加載機(jī)制(不向下兼容) (qq.com)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末夺巩,一起剝皮案震驚了整個濱河市续镇,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌逢净,老刑警劉巖甥雕,帶你破解...
    沈念sama閱讀 211,194評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異胀茵,居然都是意外死亡社露,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評論 2 385
  • 文/潘曉璐 我一進(jìn)店門琼娘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來峭弟,“玉大人附鸽,你說我怎么就攤上這事÷魅常” “怎么了坷备?”我有些...
    開封第一講書人閱讀 156,780評論 0 346
  • 文/不壞的土叔 我叫張陵,是天一觀的道長情臭。 經(jīng)常有香客問我省撑,道長,這世上最難降的妖魔是什么俯在? 我笑而不...
    開封第一講書人閱讀 56,388評論 1 283
  • 正文 為了忘掉前任竟秫,我火速辦了婚禮,結(jié)果婚禮上朝巫,老公的妹妹穿的比我還像新娘鸿摇。我一直安慰自己石景,他們只是感情好劈猿,可當(dāng)我...
    茶點故事閱讀 65,430評論 5 384
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著潮孽,像睡著了一般揪荣。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上往史,一...
    開封第一講書人閱讀 49,764評論 1 290
  • 那天仗颈,我揣著相機(jī)與錄音,去河邊找鬼椎例。 笑死挨决,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的订歪。 我是一名探鬼主播脖祈,決...
    沈念sama閱讀 38,907評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼刷晋!你這毒婦竟也來了盖高?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,679評論 0 266
  • 序言:老撾萬榮一對情侶失蹤眼虱,失蹤者是張志新(化名)和其女友劉穎喻奥,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體捏悬,經(jīng)...
    沈念sama閱讀 44,122評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡撞蚕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,459評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了过牙。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片诈豌。...
    茶點故事閱讀 38,605評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡仆救,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出矫渔,到底是詐尸還是另有隱情彤蔽,我是刑警寧澤,帶...
    沈念sama閱讀 34,270評論 4 329
  • 正文 年R本政府宣布庙洼,位于F島的核電站顿痪,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏油够。R本人自食惡果不足惜蚁袭,卻給世界環(huán)境...
    茶點故事閱讀 39,867評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望石咬。 院中可真熱鬧揩悄,春花似錦、人聲如沸鬼悠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,734評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽焕窝。三九已至蹬挺,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間它掂,已是汗流浹背巴帮。 一陣腳步聲響...
    開封第一講書人閱讀 31,961評論 1 265
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留虐秋,地道東北人榕茧。 一個月前我還...
    沈念sama閱讀 46,297評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像客给,于是被迫代替她去往敵國和親用押。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,472評論 2 348

推薦閱讀更多精彩內(nèi)容