Spring Framework升级

​SpringBoot2.2​​的底层​​Spring Framework​​版本升级为​​5.2​​。

JMX默认禁用

默认情况下不再启用JMX。 可以使用配置属性spring.jmx.enabled = true启用此功能。 如果您使用IDE功能来管理应用程序,则可能还要在该位置启用该标志。

Java 13支持

Spring Boot 2.2增加了对Java 13的支持。还支持Java 8和11。

性能提升

通过使用​​proxyBeanMethods=false​​Spring Boot的​​@Configuration​​类,减少了启动时间和内存使用量。 ​​proxyBeanMethods​​是​​@Configuration​​Spring Framework 5.2 M1中引入的新属性。 ​​proxyBeanMethods​​也可作为一个属性​​@SpringBootApplication​​和​​@SpringBootConfiguration​​。

在使用​​bootRun​​Gradle或​​spring-boot:run​​Maven 在开发时启动应用程序时,JVM将配置有标志(​​-Xverify:none​​和​​-XX:TieredStopAtLevel=1​​)以对其进行优化以减少启动时间。在JDK 13上运行时,​​-Xverify:none​​未指定,因为已弃用。

此版本中还进行了其他一些性能改进:

  • 绑定大量配置属性所需的时间已大大减少
  • 当Spring Boot ​​PersistenceUnit​​通过扫描JPA实体完全准备一个时,由于它是冗余的,因此Hibernate自己的实体扫描已被禁用
  • 自动配置中的注入点已经过改进,仅适用于必须创建bean的情况
  • 现在仅在启用和公开端点的情况下(通过JMX或HTTP)创建与Actuator端点相关的Bean。
  • 编解码器自动配置的条件已得到改善,以便在不再使用编解码器时不再对其进行配置
  • Tomcat的MBean注册表默认情况下处于禁用状态,从而将Tomcat的内存占用量减少了大约2MB

延迟初始化

现在可以通过该​​spring.main.lazy-initialization​​属性启用全局延迟初始化以减少启动时间。请注意,使用该功能可能需要付出一定的成本或者代价:

  • 在进行任何延迟的初始化时,HTTP请求的处理可能需要更长的时间
  • 现在,通常不会在启动时发生故障,直到以后

通过使用注释各自的定义,各个bean可以选择退出延迟初始化​​@Lazy(false)​​。如果无法​​@Lazy(false)​​选择退出延迟初始化,​​LazyInitializationExcludeFilter​​则可以使用Bean代替。例如,要永远不要将​​IntegrationFlow​​bean 设置为惰性,可以使用以下代码:



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


JUnit 5

​spring-boot-starter-test​​现在默认提供JUnit 5。默认情况下,包括JUnit 5的老式引擎以支持现有的基于JUnit 4的测试类,以便您可以在准备好迁移到JUnit 5时进行迁移。也可以在同一模块中混合使用基于JUnit 4和基于JUnit 5的测试类。这使您可以根据需要逐步迁移到JUnit 5。

请注意,JUnit 4的Maven Surefire插件不支持该​​listener​​属性。如果您具有类似于以下内容的Maven配置:



<configuration>
<properties>
<property>
<name>listener</name>
<value>com.example.CustomRunListener</value>
</property>
</properties>
</configuration>


 

不能使用,​​junit-vintage-engine​​而需要显式回滚到JUnit 4:



<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</exclusion>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>


 

DataSource 健康监控

该​​DataSource​​健康指标包含一个额外的​​validationQuery​​与反对使用的验证查询属性​​DataSource​​。同样,该​​hello​​属性提供的查询结果已重命名为​​result​​。

Freemarker模板配置

此版本更改了Freemarker模板的默认模板文件扩展名。这可以使Spring Boot与Web应用程序的安全配置默认值保持一致。升级时,请将现有模板从重命名​​*.ftl​​为​​*.ftlh​​。

DevTools配置目录

现在,全局DevTools设置的首选位置是​​~/.config/spring-boot​​。可以使用以下任何文件:

  • ​spring-boot-devtools.properties​
  • ​spring-boot-devtools.yaml​
  • ​spring-boot-devtools.yml​

@ConfigurationPropertiesScan

​@ConfigurationProperties​​现在可以通过类路径扫描找到带注释的类,以替代使用​​@EnableConfigurationProperties​​或​​@Component​​。添加​​@ConfigurationPropertiesScan​​到您的应用程序以启用扫描。

在Spring Boot 2.2.0中默认启用了配置属性扫描,但是从Spring Boot 2.2.1开始,您必须使用进行选择​​@ConfigurationPropertiesScan​​。

@ConstructorBinding

配置属性现在支持基于构造函数的绑定,该绑定允许带​​@ConfigurationProperties​​注释的类不可变。可以通过使用注释一个​​@ConfigurationProperties​​类或其构造函数之一来启用基于构造函数的绑定​​@ConstructorBinding​​。现在可以在配置属性绑定提供的构造函数参数上使用诸如​​@DefaultValue​​和的注释​​@DateTimeFormat​​。

Spring Boot 2.2中的弃用

  • 该​​logging.file​​属性已重命名为​​logging.file.name​​。
  • 该​​logging.path​​属性已重命名为​​logging.file.path​​。
  • ​server.connection-timeout​​不建议使用该属性,而应使用服务器特定的属性,因为它们的行为并不完全相同。
  • ​server.use-forward-headers​​不赞成使用该财产​​server.forward-headers-strategy​​; 以前​​server.use-forward-headers=true​​会使用Web服务器的本机支持。现在,您可以使用实现相同的功能​​server.forward-headers-strategy=native​​。由于每个服务器都有特定的行为,因此我们现在提供一种替代方法,该替代方法依赖于Spring的​​ForwardedHeaderFilter​​:​​server.forward-headers-strategy=framework​​。开发人员应使用最适合其用例的选项。
  • ​ReactiveWebServerApplicationContext#getWebServerFactory​​。
  • ​agent​​Maven插件的属性已重命名为​​agents​​。
  • 不赞成使用Joda的时间支持​​java.time​​。
  • ​ApplicationHealthIndicator​​赞成的​​PingHealthIndicator​​,就是始终贡献。
  • ​ConfigurationBeanFactoryMetadata​​赞成​​ConfigurationPropertiesBean​​。
  • ​ConfigurationPropertiesBindingPostProcessor​​赞成​​@EnableConfigurationProperties​​或​​register​​方法的构造函数。
  • ​ConfigurationPropertiesBindingPostProcessor.VALIDATOR_BEAN_NAME​​已移至​​EnableConfigurationProperties.VALIDATOR_BEAN_NAME​​。
  • ​ConfigurationPropertiesBindingPostProcessorRegistrar​​赞成​​@EnableConfigurationProperties​​。
  • ​WebTestClientBuilderCustomizer​​已移至​​org.springframework.boot.test.web.reactive.server​​。