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
andapplication.yml
files are processed. If you only have a simpleapplication.properties
orapplication.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 aspring.config.use-legacy-processing
property totrue
in yourapplication.properties
orapplication.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 pluginbootBuildImage
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(過濾器)。