Spring Boot 2.1 版本升级说明

Spring Boot 2.1 版本升级说明

这是官方的Spring Boot 2.1 版本升级说明,本文将对其作出个人理解。对于配置属性变更或者不常用组件的变更,过于繁琐,我就跳过了。

Upgrading from Spring Boot 2.0

Deprecations from Spring Boot 2.0

Classes, methods and properties that were deprecated in Spring Boot 2.0 have been removed in this release. Please ensure that you aren’t calling deprecated methods before upgrading.

照例说明,过期、删除的方法禁止调用。

Spring Framework 5.1

Spring Boot 2.1 uses Spring Framework 5.1. Please refer to its upgrade guide for any changes that may affect your application.

spring依赖升级到5.1。

Bean Overriding

Bean overriding has been disabled by default to prevent a bean being accidentally overridden. If you are relying on overriding, you will need to set spring.main.allow-bean-definition-overriding to true.

DefaultListableBeanFactory类在 springboot2.0 之前已经定义是否可重复注册Bean的属性,默认为true,2.1版本升级中,在 SpringApplication 类新增了allowBeanDefinitionOverriding属性,默认为false,对 DefaultListableBeanFactory 类中的属性进行了覆盖。

public class SpringApplication {
    private void prepareContext(ConfigurableApplicationContext context,
      ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
      ApplicationArguments applicationArguments, Banner printedBanner) {
        if (beanFactoryinstanceof DefaultListableBeanFactory) {
            ((DefaultListableBeanFactory) beanFactory)
            .setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
        }
    }
}

如果不允许重复定义Bean,会报错,允许则后面扫描到的会覆盖前一个。参考文章springboot2.1.0的是否可重复注册Bean配置

Actuator 'info' and 'health' Endpoint Security

If spring-security is on the classpath without any security configuration, /info and /health are now exposed publicly for consistency. If you have spring-security on your classpath and do not provide any security configuration, you will need to explicitly secure them.

如果引入了 security 依赖,但没有做任何配置,'info' and 'health' 端点默认是公开访问的。

Servlet Path

The server.servlet.path property has moved to spring.mvc.servlet.path. If you were relying on that property programmatically to know the path of the DispatcherServlet please use DispatcherServletPath instead.

配置属性变更。spring.mvc.servlet.path 属性可以为应用接口路径统一设置前缀。

Narayana JTA Support

The Narayana support has been removed in favor of the official support that is more aligned with Narayana releases. If you were using spring-boot-starter-jta-narayana, the new coordinates are the following:

<dependency>
    <groupId>me.snowdrop</groupId>
    <artifactId>narayana-spring-boot-starter</artifactId>
    <version>2.0.1</version>
</dependency>

Check the documentation for more details.

Narayana(https://narayana.io/),是由Jboss团队提供的XA分布式事务的解决方案。基于JTA实现。

ActiveMQ Pooling

If you were using activemq-pool, support has been removed in this release in favor of pooled-jms that offers the same features while being JMS 2.0 compliant. You can update your build as follows:

<dependency>
    <groupId>org.messaginghub</groupId>
    <artifactId>pooled-jms</artifactId>
</dependency>

在JMS 2.0中,移除 activemq-pool 依赖的支持(不过还在更新),增加 pooled-jms 的支持,参考文章springboot 2.0和2.1集成activemq的不同方式

HttpPutFormContentFilter

HttpPutFormContentFilter has been deprecated in favor of FormContentFilter. As a result the spring.mvc.formcontent.putfilter.enabled property is no longer defined. If you were using this feature, please update to spring.mvc.formcontent.filter.enabled.

平时我们只能接受 HTTP POST 表单请求的参数,通过 HttpPutFormContentFilter 可以接受 HTTP PUT和PATCH请求,不过现在HttpPutFormContentFilter 过期了,应该使用 FormContentFilter过滤器,并且它还支持 HTTP DELETE 请求。

InfluxDB HttpClient Customization

Previously, declaring a OkHttpClient.Builder bean was enough to customize the client used by InfluxDB. To make sure such customizations are isolated, please define a InfluxDbOkHttpClientBuilderProvider bean instead.

InfluxDB:时序数据库。
以前通过 OkHttpClient.Builder 实现自定义连接属性,现在通过 OkHttpClient.Builder 的子类 InfluxDbOkHttpClientBuilderProvider 自定义。参考文章 InfluxDbOkHttpClientBuilderProvider 自定义连接InfluxDB

Removal of 'spring.provides' Files

A starter could declare a META-INF/spring.provides so that an IDE can figure out what dependencies it provides. Scanning the starter POM for the immediate dependencies that it declares should be enough of an indication. If you are responsible of a third-party starter and you declare this file, it can be removed.

spring.provides 配置所依赖的artifactId,给IDE使用,方便引入所需依赖。2.1开始不再提供此文件,即使没有此文件也能通过扫描pom发现所需依赖。如果自己制作的starter,也应该删掉此文件。

Consistent max HTTP header size across all embedded web servers

The default max HTTP header size is now consistent across the four supported embedded web servers (Jetty, Netty, Tomcat, and Undertow) and is 8KB. The size can be customized using the server.max-http-header-size property.

注意:maxHttpHeaderSize不宜设置过大,否则在高并发情况下,容易造成OOM。

Java 11 Support

Spring Boot 2.1 remains compatible with Java 8 but now also supports Java 11. We have continuous integration configured to build and test Spring Boot against the latest Java 11 release.

Spring Boot 2.1支持JDK8和11。

DataSize Support

If a property needs to express a size in bytes or similar convenient unit, it can expose a org.springframework.util.unit.DataSize property. Similar to our Duration support introduced in Spring Boot 2.0, the data size supports an easy format (i.e. 10MB for 10 megabytes) and metadata support. All relevant configuration properties have been updated to use the new type.

DataSize 类可以用来表示数据大小,单位从 B 到 TB。springboot自身已替换完成。

Context ApplicationConversionService Support

The ApplicationConversionService is now registered by default with the Environment and BeanFactory created by SpringApplication. This allows you to use application converters directly with core Spring Framework items such as the @Value annotation:

@Value("${my.duration:10s}")
private Duration duration;

在应用启动时会配置Environment类,这个时候会获取一个 ApplicationConversionService 对象,这里面包含 String转Duration、String转DataSize等各种转换器,可以帮助@Value注解的字段实现指定数据类型注入。参考文章设置ApplicationConversionService

Profile Expression

Profile matching has been improved to support an expression format. For instance production & (us-east | eu-central) indicates a match if the production profile is active and either the us-east or eu-central profiles are active.
Profile expression can be used as follows:

Profile相关的属性值可以设置表达式,使得Profile的表示更加灵活。

Task Execution

Spring Boot now provides auto-configuration for ThreadPoolTaskExecutor. If you are using @EnableAsync, your custom TaskExecutor can be removed in favor of customizations available from the spring.task.execution namespace. Custom ThreadPoolTaskExecutor can be easily created using TaskExecutorBuilder.

ThreadPoolTaskExecutor可以直接使用spring容器实现的对象,要注意的是默认设置是个无界队列,可能产生OOM问题,可以通过属性配置修改,拒绝策略暂时不能修改。

Task Scheduling

Similarly to the new task execution support, Spring Boot auto-configures a ThreadPoolTaskScheduler when @EnableScheduling is specified. The task scheduler can be customized using the spring.task.scheduling namespace. A TaskSchedulerBuilder is also available by default.

spring容器自动装配的调度线程池,线程数默认为1,可通过属性配置修改。
例:spring.task.scheduling.pool.size=2。

Bootstrap mode for JPA setup

Spring Data Lovelace introduces a 'bootstrap mode' for the repositories. Spring Boot exposes a property that can be set to control the boostrap mode of JPA repositories. For instance, to defer initialization of JPA repositories, simply add the following to your configuration:

spring.data.jpa.repositories.bootstrap-mode=deferred

When setting the mode to deferred or lazy, JPA setup happens in a separate thread.

参考文章Spring Boot 3.x Data(五)-Spring Data JPA(配置,Bootstrap Mode,数据库初始化,命名策略)

Spring Data JDBC Support

Spring Data includes repository support for JDBC and will automatically generate SQL for the methods on CrudRepository. Spring Boot will auto-configure Spring Data’s JDBC repositories when the necessary dependencies are on the classpath. They can be added to your project with a single dependency on spring-boot-starter-data-jdbc.
For complete details of Spring Data JDBC, please refer to the reference documentation.

spring-boot-starter-data-jdbc 依赖中没有代码,帮助引入了 spring-boot-starter-jdbc 和 spring-data-jdbc 依赖。

Deprecations in Spring Boot 2.1

  • setConnectTimeout(int) and setReadTimeout(int) have been deprecated in favor of similar methods that take a Duration.

可以参考 RestTemplateBuilder 类。

  • RestTemplateBuilder.basicAuthorization has been deprecated in favor of basicAuthentication.

BasicAuthorizationInterceptor 拦截器默认使用UTF-8编码拼接Authorization Header,BasicAuthenticationInterceptor 拦截器会先判断AUTHORIZATION是否已存在,没有则会默认使用ISO-8859-1编码拼接Header。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,029评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,395评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,570评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,535评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,650评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,850评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,006评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,747评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,207评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,536评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,683评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,342评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,964评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,772评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,004评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,401评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,566评论 2 349

推荐阅读更多精彩内容