美文网首页
Spring Boot 2.3 版本升级说明

Spring Boot 2.3 版本升级说明

作者: 湖与月 | 来源:发表于2023-08-06 20:36 被阅读0次

    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 下的校验注解,则会对你产生一点影响。有些项目也会引入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版本里确定使用名称的方法没有修改,只是属性被给予了默认值true,只有手动设置为false即可。

    Jackson

    This release upgrades to Jackson 2.11 which includes a change to the default formatting of java.util.Date and java.util.Calendar. Please see FasterXML/jackson-databind#2643 for details.

    时间 1970-01-01T01:00:00.123+0100 中的+0100 是指时区影响下的时间偏移量,0000是UTC,0100则是东一区,加一小时。java8和joda time都是 +01:00格式,而jackson还保留老格式。在2.11版本中与java8统一格式了。看这里的时间实现代码

    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 已经废弃了,这是跟国外云平台部署有关的项目,国内用到的应该很少。

    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.

    配置属性变更,现在线程配置有 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 WebServerInitializedEvent is now published before the ContextRefreshedEvent.

    事件的启动顺序为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 现在支持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 using spring.lifecycle.timeout-per-shutdown-phase. Please see the reference documentation for further details.

    配置属性 server.shutdown 可以开启优雅关机,等待请求完成后,再终止应用,spring.lifecycle.timeout-per-shutdown-phase 可以设置等待,就像锁等待时间一样,如果超时则强行关闭应用。

    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 是一个通过反应式编程操作、连接数据库的驱动程序。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协议采用二进制点对点的数据传输,主要用于分布式架构中,是一种基于Reactive Streams规范标准实现的新的网络通信第七层(应用层)协议。参考文章SpringBoot使用RSocket协议

    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是新添加的类型转换服务类,作为字符串到Period类的转换器被添加到 ApplicationConversionService 类里,ApplicationConversionService 类在应用启动的时候会被实例化,作为配置属性以及应用上下文属性解析的工具。

    推荐文章

    Spring Boot 2.3.0正式发布:优雅停机、配置文件位置通配符新特性一览 (qq.com)

    相关文章

      网友评论

          本文标题:Spring Boot 2.3 版本升级说明

          本文链接:https://www.haomeiwen.com/subject/ztcqpdtx.html