Spring Boot 2.2中有什么新功能?

本文的翻译是专门为“ Spring Framework开发人员 ”课程的学生准备的。




2019年10月16日,Spring Boot 2.2发布了!

在本文中,您将了解2.2版为您提供的许多新功能。

马文


开始使用Spring Boot 2.2。 在您的应用程序中,升级到新版本的starter-parent

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>your-very-cool-project</artifactId> <version>0.0.1-SNAPSHOT</version> <name>your-very-cool-project</name> <description>Spring Boot 2.2 Project</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project> 


如果要从头开始应用程序,请在start.spring.io上创建一个Maven或Grade项目!



全局懒豆初始化


Spring框架长期以来一直支持bean组件的延迟初始化。

延迟初始化的bean告诉IoC容器在第一个请求而不是在启动时实例化该bean。


Spring Boot 2.2中引入的新功能支持Bean组件的全局延迟初始化 (默认情况下禁用此功能)。

打开全局惰性bean初始化会发生什么?

  • 所有Spring Bean及其依赖项的初始化将延迟到需要它们之前。


为了减少应用程序启动时间,现在可以使用以下命令在配置属性中启用Spring Bean的全局延迟初始化:

 spring.main.lazy.initialization=true 


或yml配置:

 spring: main: lazy-initialization: true 


使用惰性bean初始化有什么缺点吗? 当然可以! 了解后果非常重要-在不考虑此解决方案的情况下,不应包括Bean组件的全局初始化! 有一些折衷考虑:

  • 在进行任何延迟初始化时,处理HTTP请求可能会花费更长的时间。 后续请求不受影响。
  • 现在通常不会在启动时发生故障(因为在创建应用程序上下文时会创建Spring Bean及其依赖项)。 因此,您的应用程序在启动时将不再产生明显的崩溃! 结果:您的客户可能是第一个遇到问题的用户(bean接线)。


如果您不想全局启用bean的延迟初始化( spring.main.lazy.initialization = false ),则可以考虑使用@Lazy注释为特定组件设置延迟初始化。

另一方面,还可以在全局级别启用bean组件的延迟初始化( spring.main.lazy.initialization = true ),并显式禁用特定组件的延迟初始化。

总结一下:





默认情况下禁用JMX


从Spring Boot 2.2开始,默认情况下禁用JMX。 这有助于减少应用程序启动时间,并且不会在运行时浪费大量资源。 如果仍然依赖JMX,则可以再次启用它:

 spring.jmx.enabled=true 


或:

 spring: jmx: enabled: true 


配置属性的改进


Spring Boot 2.2对配置属性进行了一些不错的改进。

  • @ConfigurationProperties扫描支持
  • 不变的绑定@ConfigurationProperties


@ConfigurationProperties 类路径扫描支持


Spring Boot将为每个配置类创建一个bean,该类在扫描类路径时以@ConfigurationProperties注释。 这是替代使用

  • @EnableConfigurationProperties用于绑定属性类的类
  • 或在配置类中使用@Component批注使其成为bean。


请记住,可以为配置类创建两个bean组件,并使用@Component@ConfigurationProperties注释。 在这种情况下,应从配置类中删除@Component

不变的绑定@ConfigurationProperties


现在,使用基于构造函数的属性绑定支持不可变的配置属性类。 可以通过使用@ConstructorBinding注释@ConfigurationProperties类或其构造函数之一来启用基于构造函数的@ConfigurationProperties

例如:

 package com.example.immutable.configuration.binding; import lombok.Getter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConstructorBinding; @Getter @ConfigurationProperties("stock.quote.subscription") public class ImmutableStockQuoteSubscriptionProperties { private final String endpoint; private final String apiKey; private final SubscriptionType subscriptionType; private final boolean enabled; @ConstructorBinding public ImmutableStockQuoteSubscriptionProperties(String endpoint, String apiKey, SubscriptionType subscriptionType, boolean enabled) { this.endpoint = endpoint; this.apiKey = apiKey; this.subscriptionType = subscriptionType; this.enabled = enabled; } enum SubscriptionType { /** * Real time stock quotes */ REALTIME, /** * Stock quotes that are delayed by 15 minutes */ DELAYED, } } 


我的application.yml中的属性:

 stock: quote: subscription: endpoint: http://stockgeeks.io/api enabled: true apiKey: secret subscriptionType: realtime 


有关更多详细信息,请参见文档

执行器端点变化


/actuator/health端点通过将details重命名为第一级元素的components来更改最终的JSON格式。

执行器媒体类型已从: application/vnd.spring-boot.actuator.v2+json更改为application/vnd.spring-boot.actuator.v3+json

Spring Boot 2.2(Actuator V2)之前的示例端点响应/actuator/health

 { "status": "UP", "details": { "db": { "status": "UP", "details": { "database": "HSQL Database Engine", "result": 1, "validationQuery": "SELECT COUNT(*) FROM INFORMATION_SCHEMA.SYSTEM_USERS" } }, "diskSpace": { "status": "UP", "details": { "total": 250685575168, "free": 32597131264, "threshold": 10485760 } }, "ping": { "status": "UP" } } } 


端点答案/actuator/health Spring Boot 2.2(执行器V3):

 { "status": "UP", "components": { "db": { "status": "UP", "details": { "database": "HSQL Database Engine", "result": 1, "validationQuery": "SELECT COUNT(*) FROM INFORMATION_SCHEMA.SYSTEM_USERS" } }, "diskSpace": { "status": "UP", "details": { "total": 250685575168, "free": 32605003776, "threshold": 10485760 } }, "ping": { "status": "UP" } } } 


您的工具箱可能取决于运行状况执行器V2格式。

Spring Boot 2.2运行状况通过指定HTTP Accept标头向后兼容:使用V2媒体类型, application/vnd.spring-boot.actuator.v2+json

您可以使用curl自己进行操作:

curl -H "Accept: application/vnd.spring-boot.actuator.v2+json" http://localhost:8080/actuator/health

随着这一变化,现在还可以将绩效指标分组

RSocket支持


RSocket是用于传输字节流的二进制通信协议。 通过单个通道上的异步消息传递,可以实现对称的交互模型。

除了用于RSocket的新启动器外,还添加了广泛的自动配置。

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-rsocket</artifactId> </dependency> 


有关更多信息,请阅读RSocket Spring Boot文档。

Java支持13


Java 13于2019年9月17日发布。

Spring Framework 5.2和Spring Boot 2.2现在支持Java 13。

Java LTS版本8和11将与Spring Boot 2.2保持兼容。

Kubernetes云平台发现


ConditionalOnCloudPlatform现在确定您的Spring Boot应用程序是否在Kubernetes中运行。

 package nl.jtim.spring.boot; import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform; import org.springframework.boot.cloud.CloudPlatform; import org.springframework.stereotype.Service; @Service @ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES) public class MyVeryCoolService { } 


标语


Spring Boot带有一个默认横幅,该横幅在应用程序启动后立即出现在控制台中。



这是一个非常令人讨厌的功能,但是Spring Boot已经支持(动画)自定义横幅。

从Spring Boot 2.2开始,您可以使横幅更好:

  • ASCII标语文件现在可以使用{AnsiColor.NNN}(其中NNN是颜色代码 )使用ANSI 256颜色控制字符。
  • 现在,对于(动画)图形横幅,您可以将spring.banner.image.bitdepth属性设置为8 。 以及使用块ASCII字符的blockspring.banner.image.pixelmode属性。


结果看起来像这样:



来源: https : //github.com/spring-projects/spring-boot/wiki/images/animated-ascii-art-256.gif

有关改进的Spring Boot 2.2动画动画横幅的简单示例,请参见我的Github示例

从Java EE迁移到Jakarta EE


在可能的情况下,Spring(引导)命令从Java EE依赖项切换为组标识符为javax 。 Spring Boot启动程序中具有jakarta组ID的Jakarta EE等效依赖项。

此外,还添加了Jakarta EE依赖项API的依赖项管理以及Java EE依赖项API的现有依赖项管理。

请记住,在将来的Spring Boot版本中将删除Java EE API依赖关系管理,建议您开始使用Jakarta EE API依赖关系。

配置密钥更改


Spring Boot 2.2引入了许多新的配置密钥。 也有一些过时和删除的键。 太多的更改无法涵盖所有​​这些更改,但是以下是一些重要的更改:

logging.file已重命名为logging.file.name
logging.path已重命名为logging.file.path

有关所有更改的完整概述,请参见Spring Boot 2.2配置更改日志!

过时的


请查看发行说明,以获取不推荐使用的类和属性的完整列表。

应注意的一些变化:

  • 不赞成使用Joda时间支持,而建议使用java.time
  • 不建议使用Elasticsearch传输客户端和Jest客户端,而应使用其他选项,例如RestHighLevelClient和ReactiveElasticsearchClient,有关更多详细信息,请参阅文档


依赖关系更新


Spring Boot 2.2附带了许多更新的依赖项。

Spring依赖关系更新:



对其他依赖项的最重要更新:



测试依赖项更新:



升级到Spring Boot 2.2


自2019年8月1日起,Spring Boot 1.x已完成其生命周期 。 如果您仍在使用Spring Boot 1.x应用程序,那么该进行升级了!

请记住,Spring Boot 2.1中已删除了Spring Boot 2.1中不推荐使用的类,方法和属性。 确保升级之前不要调用不赞成使用的方法。 查看Spring Boot 2.1中不推荐使用的发行说明

您可以在以下链接中找到更多信息:



如果您发现本文有帮助,请单击+。

有任何问题或反馈吗?
搜索Twitter: @TimvanBaarsen

Source: https://habr.com/ru/post/zh-CN475384/


All Articles