Spring Boot 2.3 版本升級說明
這是官方的Spring Boot 2.3 版本升級說明,本文將對其作出個人理解。對于配置屬性變更或者不常用組件的變更,過于繁瑣,我就跳過了。
Changes to minimum requirements
Spring Boot now requires:
- Gradle 6.3+ (if you are building with Gradle). 5.6.x is also supported but in a deprecated form.
- Jetty 9.4.22+ (if you are using Jetty as the embedded container)
Gradle和Jetty的依賴最低要求版本提升了。
Validation Starter no longer included in web starters
As of #19550, Web and WebFlux starters do not depend on the validation starter by default anymore. If your application is using validation features, for example you find that
javax.validation.*imports are not being resolved, you’ll need to add the starter yourself.
For Maven builds, you can do that with the following:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
spring-boot-starter-web 不再包含依賴 spring-boot-starter-validation,需要手動引入。如果你有使用@Valid、@NotNull 等 javax.validation 下的校驗注解,則會對你產(chǎn)生一點影響。有些項目也會引入hibernate-validator (groupId:org.hibernate.validator)依賴代替,基本沒有差別。
Unique DataSource Name By Default
By default, a unique name is generated on startup for the auto-configured DataSource. This impacts the use of the H2 console as the database URL no longer refers to testdb.
You can disable this behavior by setting spring.datasource.generate-unique-name to false.
public String determineDatabaseName() {
if (this.generateUniqueName) {
if (this.uniqueName == null) {
this.uniqueName = UUID.randomUUID().toString();
}
return this.uniqueName;
}
if (StringUtils.hasLength(this.name)) {
return this.name;
}
if (this.embeddedDatabaseConnection != EmbeddedDatabaseConnection.NONE) {
return "testdb";
}
return null;
}
2.2和2.3版本里確定使用名稱的方法沒有修改,只是屬性被給予了默認(rèn)值true,只有手動設(shè)置為false即可。
Jackson
This release upgrades to Jackson 2.11 which includes a change to the default formatting of
java.util.Dateandjava.util.Calendar. Please see FasterXML/jackson-databind#2643 for details.
時間 1970-01-01T01:00:00.123+0100 中的+0100 是指時區(qū)影響下的時間偏移量,0000是UTC,0100則是東一區(qū),加一小時。java8和joda time都是 +01:00格式,而jackson還保留老格式。在2.11版本中與java8統(tǒng)一格式了。看這里的時間實現(xiàn)代碼
Spring Cloud Connectors starter has been removed
The Spring Cloud Connectors starter was deprecated in 2.2 in favor of Java CFEnv. This starter has been removed, and Spring Cloud Connectors dependencies are no longer included in Spring Boot’s managed dependencies.
依賴 spring-boot-starter-cloud-connectors 已經(jīng)廢棄了,這是跟國外云平臺部署有關(guān)的項目,國內(nèi)用到的應(yīng)該很少。
Embedded Servlet web server threading configuration
The configuration properties for configuring the threads used by embedded Servlet web servers (Jetty, Tomcat, and Undertow) have moved to dedicated threads groups. The properties can now be found in server.jetty.threads, server.tomcat.threads, and server.undertow.threads. The old properties remain in a deprecated form to ease migration.
配置屬性變更,現(xiàn)在線程配置有 server.tomcat.threads.minSpare 和server.tomcat.threads.max。
ApplicationContextRunner disables bean overriding by default
For consistency with SpringApplication, ApplicationContextRunner now disables bean overriding by default. If you need to use bean overriding for a test, withAllowBeanDefinitionOverriding can be used to enable it.
ApplicationContextRunner是一個測試輔助類,參考文章[分享一個 Spring Boot 提供的測試輔助類 ApplicationContextRunner]
(https://blog.csdn.net/weixin_42189048/article/details/124847651)。
WebServerInitializedEvent and ContextRefreshedEvent
As part of introducing support for graceful shutdown, web server initialisation is now performed at the end of application context refresh processed rather than immediately after refresh processing has completed. As a result, the
WebServerInitializedEventis now published before theContextRefreshedEvent.
事件的啟動順序為WebServerInitializedEvent在ContextRefreshedEvent之前,參考文章Spring 常用的一些事件。
Java 14 support
Spring Boot 2.3 adds support for Java 14. Java 8 and 11 are also supported.
Spring Boot 2.3 現(xiàn)在支持java8、java11、java14。
Graceful shutdown
Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and Servlet-based web applications. When enabled using
server.shutdown=graceful, upon shutdown, the web server will no longer permit new requests and will wait for a grace period for active requests to complete. The grace period can be configured usingspring.lifecycle.timeout-per-shutdown-phase. Please see the reference documentation for further details.
配置屬性 server.shutdown 可以開啟優(yōu)雅關(guān)機,等待請求完成后,再終止應(yīng)用,spring.lifecycle.timeout-per-shutdown-phase 可以設(shè)置等待,就像鎖等待時間一樣,如果超時則強行關(guān)閉應(yīng)用。
Spring Data Neumann
R2DBC support
When r2dbc is on the classpath, a ConnectionFactory is auto-configured with a similar arrangement than a jdbc DataSource. If Spring Data is on the classpath, repositories are auto-configured as well, as usual.
R2DBC support also adds an health indicator for the connection factory, metrics for ConnectionPool and a test slice, @DataR2dbcTest.
R2DBC 是一個通過反應(yīng)式編程操作、連接數(shù)據(jù)庫的驅(qū)動程序。2022起步不久。參考文章Spring Data R2DBC快速上手指南。
Date-Time conversion in web applications
The conversion of time and date-time values in web applications is now configurable via application properties. This complements that existing support for formatting date values. For MVC, the properties are spring.mvc.format.time and spring.mvc.format.date-time respectively. For WebFlux, the properties are spring.webflux.format.time and spring.webflux.format.date-time respectively.
In addition to taking a typical formatting pattern, the properties for configuring the formatting of dates, times, and date-times now support a value of iso. When set, the corresponding ISO-8601 formatting will be applied.
The iso values is supported by the following properties:
- spring.mvc.format.date
- spring.mvc.format.date-time
- spring.mvc.format.time
- spring.webflux.format.date
- spring.webflux.format.date-time
- spring.webflux.format.time
在spring boot項目里試了下,并不生效,只有加上jackson的配置才有效。不明白是什么原因。
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
RSocket support for Spring Integration
Spring Boot now provides auto-configuration for Spring Integration’s RSocket support.
If spring-integration-rsocket is available, developers can configure an RSocket server using "spring.rsocket.server.*" properties and let it use IntegrationRSocketEndpoint or RSocketOutboundGateway components to handle incoming RSocket messages.
RSocket協(xié)議采用二進制點對點的數(shù)據(jù)傳輸,主要用于分布式架構(gòu)中,是一種基于Reactive Streams規(guī)范標(biāo)準(zhǔn)實現(xiàn)的新的網(wǎng)絡(luò)通信第七層(應(yīng)用層)協(xié)議。參考文章SpringBoot使用RSocket協(xié)議。
Binding to Period
If a property needs to express a period of time, you can do so using a java.time.Period property. Similar to the Duration support, an easy format is supported (i.e. 10w for 10 weeks) as well as metadata support.
StringToPeriodConverter是新添加的類型轉(zhuǎn)換服務(wù)類,作為字符串到Period類的轉(zhuǎn)換器被添加到 ApplicationConversionService 類里,ApplicationConversionService 類在應(yīng)用啟動的時候會被實例化,作為配置屬性以及應(yīng)用上下文屬性解析的工具。