Spring Boot 2.5 版本升级说明
这是官方的Spring Boot 2.5 版本升级说明,本文将通过个人理解以及文心一言的回答作出解释。对于配置属性变更或者不常用组件的变更,过于繁琐,我就跳过了。
SQL Script DataSource Initialization
The underlying method used to support schema.sql and data.sql scripts has been redesigned in Spring Boot 2.5. spring.datasource.* properties related to DataSource initialization have been deprecated in favor of new spring.sql.init.* properties. These properties can also be used to initialize an SQL database accessed via R2DBC.
配置属性 spring.sql.init.* 代替 spring.datasource.*,是功能细化的表现,数据库初始化的功能移到了 sql.init 目录下。
schema.sql and data.sql Files
With Spring Boot 2.5.1 and above, the new SQL initialization properties support detection of embedded datasources for JDBC and R2DBC. By default, SQL database initialization is only performed when using an embedded in-memory database. To always initialize a SQL database, irrespective of its type, set spring.sql.init.mode to always. Similarly, to disable initialization, set spring.sql.init.mode to never.
配置属性 spring.sql.init.mode 在 SqlInitializationProperties 类中,使用他可以控制应用启动时是否初始化数据源,如果不,则会在第一次数据库请求时初始化。参考文章Springboot启动(DataSources创建时)执行初始化SQL脚本 – 源码剖析。
Separate Credentials
The new script-based SQL database initialization does not support using separate credentials for schema (DDL) and data (DML) changes. This reduces complexity and aligns its capabilities with Flyway and Liquibase. If you require separate credentials for schema and data initialization, define your own org.springframework.jdbc.datasource.init.DataSourceInitializer beans.
这里的 "separate credentials" 指的是用户名和密码(凭据)。在新的基于脚本的SQL数据库初始化方式中,不支持为DDL和DML操作使用不同的凭据,这意味着对于模式和数据初始化,需要使用相同的数据库用户名和密码。如果需要为DDL和DML操作使用不同的凭据,开发者需要自定义DataSourceInitializer bean来实现这一功能。
Hibernate and data.sql
By default, data.sql scripts are now run before Hibernate is initialized. This aligns the behavior of basic script-based initialization with that of Flyway and Liquibase. If you want to use data.sql to populate a schema created by Hibernate, set spring.jpa.defer-datasource-initialization to true. While mixing database initialization technologies is not recommended, this will also allow you to use a schema.sql script to build upon a Hibernate-created schema before it’s populated via data.sql.
这个改变使得data.sql脚本默认在Hibernate初始化之前运行,但如果需要将Hibernate和脚本初始化结合起来使用,可以通过设置spring.jpa.defer-datasource-initialization为true来实现。然而,官方并不推荐混合使用不同的数据库初始化技术。
Initialization ordering
Beans of certain well-known types, such as
JdbcOperations
, will be ordered so that they are initialized after the database has been initialized. If you have a bean that works with theDataSource
directly, annotate its class or@Bean
method with@DependsOnDatabaseInitialization
to ensure that it too is initialized after the database has been initialized.
@DependsOnDatabaseInitialization注解可以确保所在类或Bean会在数据库初始化后再加载,从而保证应用程序的正常运行。
Logging Shutdown Hooks
We now register a logging shutdown hook by default for jar based applications to ensure that logging resources are released when the JVM exits. If your application is deployed as a war then the shutdown hook is not registered since the servlet container usually handles logging concerns.
Most applications will want the shutdown hook. However, if your application has complex context hierarchies, then you may need to disable it. You can use the logging.register-shutdown-hook property to do that.
默认情况下,基于jar的应用程序会注册一个日志关闭钩子,以确保在JVM退出时释放日志资源。如果应用程序作为war文件部署,则不会注册关闭钩子,因为servlet容器通常会处理日志问题。
看到网上有文章指出这个日志关闭钩子会因为并行问题导致少部分日志没有打印,不过在Spring Boot 2.5.1已经修复了。参考文章springboot2.5日志系统的设计缺陷。
Environment Variable Prefixes
It’s now possible to specify a prefix for system environment variables so that you can run multiple different Spring Boot applications in the same environment. Use SpringApplication.setEnvironmentPrefix(…) to set the prefix that you want to use when binding properties.
For example, the following will add a myapp prefix:
SpringApplication application = new SpringApplication(MyApp.class);
application.setEnvironmentPrefix("myapp");
application.run(args);
All properties will now expect a prefixed version. For example, to change the server port you can set MYAPP_SERVER_PORT.
可以给系统环境变量加前缀。为系统变量添加前缀的主要作用是在同一环境中运行多个不同的Spring Boot应用程序时,避免变量冲突。
Java 16 Support
This release provides support and is tested against Java 16. Spring Boot 2.5 remains compatible with Java 8.
开始支持java16。