当前位置: 首页>后端>正文

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:

  • Enable componets using @Profile
  • Load multi-profile YAML Documents
  • Enable Logback configuration using <springProfile>

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。


https://www.xamrdz.com/backend/3h61923385.html

相关文章: