本文的翻译是专门为“ 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 { REALTIME, DELAYED, } }
我的application.yml中的属性:
stock: quote: subscription: endpoint: http:
有关更多详细信息,请参见
文档 。
执行器端点变化
/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字符的block
的spring.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