Spring Boot 2.2 版本升级说明

Spring Boot 2.2 版本升级说明

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

Spring Framework 5.2

This release upgrades to Spring Framework 5.2. Please refer to Spring Framework’s upgrade documentation for further details.

spring依赖升级到5.2。

JMX now disabled by default

As of #16090, JMX is not enabled by default anymore. This feature can be enabled with the configuration property spring.jmx.enabled=true. If you’re using IDE features to administer your applications, you may want to enable that flag there as well.

JMX 因为用的人少,所以改为默认关闭此功能,有需要的再自行开启。

Jakarta EE dependencies

Where possible, we have moved from Java EE dependencies with a javax. group ID to the equivalent Jakarta EE dependencies with a jakarta. group ID in Spring Boot’s starters. Dependency management for the Jakarta EE API dependencies has been added alongside the existing dependency management for the Java EE API dependencies. The dependency management for the Java EE API dependencies will be removed in the future and all users are encouraged to move to the Jakarta EE API dependencies.
As part of the Java EE to Jakarta EE migration, two dependencies have changed artifact ID in their latest maintenance releases. com.sun.mail:javax.mail is now com.sun.mail:jakarta.mail and org.glassfish:javax.el is now org.glassfish:jakarta.el. In the unlikely event that you were using either of these dependencies directly, please update your pom.xml or build.gradle accordingly.

Oracle将Java EE交给开源组织,Eclipse基金会接手。但Oracle不允许开源组织使用Java名号,所以Jakarta EE名称于2018.02.26应运而生。
相应地一些依赖包路径也要跟着改,这需要一个过渡期。

AssertJ 3.12

This release upgrades to AssertJ 3.12 that contains a breaking API change for assertions related to Iterator. Please see the AssertJ release notes for details.

断言的第三方实现依赖升级。

Elasticsearch

This release upgrades to Elasticsearch 6.7 which contains some breaking API changes. Please see the 6.5, 6.6 and 6.7 breaking changes pages for details.
Both the Elasticsearch transport and Jest clients have been deprecated in favor of other options, such as RestHighLevelClient. The reference docs on Elasticsearch support have been updated.

总之就是直接引入spring-boot-starter-data-elasticsearch就够了。

Gradle requirements

The minimum requirements for Gradle have changed - Spring Boot now requires Gradle 4.10+. Please upgrade accordingly.

Gradle版本要求4.10以上。

Tomcat’s MBean Registry

Tomcat’s MBean Registry is now disabled by default, saving approximately 2MB of heap. If you want to use Tomcat’s MBeans, for example so that they can be used to expose metrics via Micrometer, set the server.tomcat.mbeanregistry.enabled to true.

MBean是一个被管理的Java对象,就像Javabean组件一样,但是它遵从JMX规范的设计模式。MBean可以表示设备、应用或者任何需要被管理的资源。
参考文章Tomcat获取MBean。

HttpHiddenMethodFilter disabled by default

The filter that handles the _method request parameter is now disabled by default as it causes early consumption of a request body if the body may contain parameters. This can be restored by setting either spring.webflux.hiddenmethod.filter.enabled or spring.mvc.hiddenmethod.filter.enabled to true.

浏览器form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求,该过滤器为HiddenHttpMethodFilter。参考文章Spring MVC过滤器-HiddenHttpMethodFilter。

New and Noteworthy

Tip:Check the configuration changelog for a complete overview of the changes in configuration. |

这里包含了新旧配置属性,可以据此查缺补漏。

Java 13 support

Spring Boot 2.2 adds support for Java 13. Java 8 and 11 are also supported.

Spring Boot 2.2支持jdk8、11、13。

Performance improvements

Startup time and memory usage have been reduced by making use of proxyBeanMethods=false in Spring Boot’s @Configuration classes. proxyBeanMethods is a new attribute on @Configuration introduced in Spring Framework 5.2 M1. proxyBeanMethods is also available as an attribute on @SpringBootApplication and @SpringBootConfiguration.

Spring在5.2版本中引入了proxyBeanMethods属性,且默认值为true,即默认会对配置了@Configuration的类生成代理对象,false则不会生成代理类。当使用代理类获取Bean对象时,会判断spring容器是否已存在,存在则返回已有对象,保持单例状态。如果直接获取Bean对象,则会出现重复创建的情况。
这个设置的初衷正如标题所说为了提升性能,大家自行选择,当然也不用怕出错,经过测试,在IDEA2022.3上如果出现重复创建的情况,会有红色警告,可据此debug。参考文章@Configuration属性proxyBeanMethods详解
刨析Spring的@Configuration注解属性proxyBeanMethods原理。

When launching an application at development time with bootRun in Gradle or spring-boot:run in Maven, the JVM will be configured with flags (-Xverify:none and -XX:TieredStopAtLevel=1) to optimise it for reduced launch time. When running on JDK 13 -Xverify:none is not specified as it has been deprecated.

java类加载过程中的一步为校验,此阶段主要确保Class文件的字节流中包含的信息符合当前虚拟机的要求,并且不会危害虚拟机的自身安全。-Xverify:none 则会关闭此校验,谨慎使用。参考文章类加载机制与JVM -Xverify参数关系。

Several other performance improvements have also been made in this release:

  • Time taken to bind large numbers of configuration properties has been significantly reduced
  • As Spring Boot fully prepares a PersistenceUnit by scanning JPA entities, Hibernate’s own entity scanning has been disabled as it is redundant
  • Injection points in auto-configurations have been refined to only apply when a bean has to be created
  • Beans related to Actuator endpoints are now only created if the endpoint is both enabled and exposed (via JMX or HTTP)
  • Conditions on codec auto-configuration have been improved so that the codecs are no longer configured when they won’t be used
  • Tomcat’s MBean Registry has been disabled by default, reducing Tomcat’s memory footprint by approximately 2MB
Lazy initialization

It is now possible to enable global lazy initialization to reduce startup time via the spring.main.lazy-initialization property. Please note that using that feature comes at a cost:

  • Handling of HTTP requests may take longer while any deferred initialisation occurs
  • Failures that would normally occur at startup will now not occur until later
    延迟Bean的初始化,可以提供启动速度,但是会降低初次请求的速度以及隐藏初始化过程中的bug,只适合在本地开发时测试用。

Individual beans can opt out of lazy initialization by annotating their definition with @Lazy(false). Where it is not possible to use @Lazy(false) to opt out of lazy initialization, a LazyInitializationExcludeFilter bean can be used instead. For example, to never set IntegrationFlow beans to lazy, you can use the following code:

@Bean
static LazyInitializationExcludeFilter integrationLazyInitExcludeFilter() {
    return LazyInitializationExcludeFilter.forBeanTypes(IntegrationFlow.class);
}

可以通过配置属性设置全局懒加载,然后通过@Lazy注解或者 LazyInitializationExcludeFilter 排除指定的Bean,参考文章Spring-Boot全局懒加载机制解析。

@ConfigurationProperties scanning

Classes annotated with @ConfigurationProperties can now be found via classpath scanning as an alternative to using @EnableConfigurationProperties or @Component. Add @ConfigurationPropertiesScan to your application to enable scanning.

Spring Boot 2.2.0之前,需要使用如@Component注解提醒springboot将该属性配置类注入容器,Spring Boot 2.2.0只要一个@ConfigurationProperties就行,但是Spring Boot 2.2.1开始又要配合@ConfigurationPropertiesScan注解自动注入。有点莫名其妙。

Immutable @ConfigurationProperties binding

Configuration properties now support constructor-based binding, which allows a @ConfigurationProperties-annotated class to be immutable. Constructor-based binding can be enabled by annotating a @ConfigurationProperties class or one of its constructors with @ConstructorBinding. Annotations such as @DefaultValue and @DateTimeFormat can now be used on constructor parameters that are provided by configuration property binding. Please see the relevant section of the reference documentation for further details.

@ConstructorBinding 注解通过构造函数而不是set方法注入配置属性,参考文章@ConstructorBinding介绍。

RSocket Support

A new Spring Boot starter has been added in this release, spring-boot-starter-rsocket. This starter brings the required dependencies for building an application that uses RSocket support. RSocket strategies are auto-configured to provide the required infrastructure for encoding and decoding RSocket payloads using CBOR and JSON. An RSocketRequester.Builder is auto-configured to allow you to create an RSocketRequester to send requests to remote RSocket services. For more on that, check out the relevant section if the reference documentation.
Auto-configuration for Spring Security’s RSocket integration is now available when its spring-security-rsocket module is on the classpath. The auto-configuration will enable RSocket security and configure the server RSocket factory with Spring Security’s interceptor.
When testing an RSocket server application, the local.rsocket.server.port property is set to the port that the RSocket server is listening on. It can be injected into test classes using @LocalRSocketServerPort.

RSocket协议是由Facebook、Netifi和Privota等公司开发的一个新的通讯协议,该协议采用二进制点对点的数据传输,主要用于分布式架构中,是一种基于Reactive Streams规范标准实现的新的网络通信第七层(应用层)协议。
RSocket协议具有多路复用(Multiplexed)、双向流(Bidirectional Streaming)、流控(Flow Control)、连接恢复(Socket Resumption)、异步消息传递(Asynchronous Message Passing)、传输层解耦和(Transport independent)等特点。
参考文章SpringBoot使用RSocket协议。

ApplicationContextRunner simple bean registration

The ApplicationContextRunner test utility now allows to register bean inline, see withBean for more details.

参考介绍分享一个 Spring Boot 提供的测试辅助类 ApplicationContextRunner。

你可能感兴趣的:(Spring Boot 2.2 版本升级说明)