Spring Boot 2.4 版本升级说明

Spring Boot 2.4 版本升级说明

这是官方的Spring Boot 2.4 版本升级说明,本文将通过本人以及文心一言的回答作出个人理解的解释。对于配置属性变更或者不常用组件的变更,过于繁琐,我就跳过了。

JUnit 5’s Vintage Engine Removed from spring-boot-starter-test

If you upgrade to Spring Boot 2.4 and see test compilation errors for JUnit classes such as org.junit.Test, this may be because JUnit 5’s vintage engine has been removed from spring-boot-starter-test. The vintage engine allows tests written with JUnit 4 to be run by JUnit 5. If you do not want to migrate your tests to JUnit 5 and wish to continue using JUnit 4, add a dependency on the Vintage Engine, as shown in the following example for Maven:

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

If you are using Gradle, the equivalent configuration is shown in the following example:

testImplementation("org.junit.vintage:junit-vintage-engine") {
    exclude group: "org.hamcrest", module: "hamcrest-core"
}

Spring Boot 2.4开始,junit 5 正式移除了对junit 4的兼容依赖,如果还想使用 junit 4的方法,需要手动引入兼容依赖。

Config File Processing (application properties and YAML files)

Spring Boot 2.4 has changed the way that application.properties and application.yml files are processed. If you only have a simple application.properties or application.yml file, your upgrade should be seamless. If, however, you’ve have a more complex setup (with profile specific properties, or profile activation properties) you may need to make some changes if you want to use the new features.
If you just want Spring Boot 2.3 compatible logic, you can set a spring.config.use-legacy-processing property to true in your application.properties or application.yml file.

1.配置属性 spring.profiles 过期,替换为 spring.config.activate.on-profile(给子配置文件命名用)。
2.配置属性 spring.profiles.include 虽然没有过期,但是已经不能正常使用,需要替换为 spring.profiles.group。
3.在单个配置文件内,配置参数是按在配置文件中定义的先后顺序进行加载的,后激活加载的参数会覆盖前面的。
4.jar外部化配置优先于内部的配置参数。
参考文章Spring Boot 2.4.0 发布,配置文件重大调整,不要乱升级!!

Default Servlet Registration

Spring Boot 2.4 will no longer register the DefaultServlet provided by your servlet container. In most applications, it isn’t used since the Spring MVC’s DispatcherServlet is the only servlet that’s required.
You can set server.servlet.register-default-servlet to true if you find you still need the default servlet.

DefaultServlet的作用就是兜底(拦截/),当别的servlet都没匹配上时就交给它来处理,一般用于处理静态资源如.jpg,.html,.js这类的静态文件。
但是Spring Boot注册的DispatcherServlet的path也是/(覆盖掉了DefaultServelt)。
现在的Spring Boot服务大都是REST服务,并无静态资源需要提供,因此就没有必要启用DefaultServletHttpRequestHandler和注册DefaultServlet来增加不必要的开销喽。
参考文章Spring Boot 2.4.0正式发布,全新的配置文件加载机制(不向下兼容)

HTTP traces no longer include cookie headers by default

Cookie request headers and Set-Cookie response headers are no longer included in HTTP traces by default. To restore Spring Boot 2.3’s behaviour, set management.trace.http.include to cookies, errors, request-headers, response-headers.

Http Trace 是跟踪记录请求信息的功能,用于在actuator中显示最近http请求信息。

Removal of Plugin Management for Flatten Maven Plugin

Spring Boot’s build no longer makes use of the Flatten Maven Plugin (flatten-maven-plugin) and plugin management for it has been removed. If you were relying on Spring Boot’s managed version, you should add your own plugin management.

flatten-maven-plugin是一个解决项目版本号管理问题的插件。当使用占位符统一版本号管理时,maven无法识别占位符,就可以加入此插件。参考文章Maven 版本管理与 flatten-maven-plugin 插件的使用及分析

Version management for exec-maven-plugin

The version management for the exec-maven-plugin has been removed. If you are using this plugin, make sure to specify a version in your own pluginManagement.

exec-maven-plugin能够帮助我们在Maven项目构建过程中执行外部操作。参考文章exec-maven-plugin的使用详解

Spring Framework 5.3

Spring Boot 2.4 uses Spring Framework 5.3. The Spring Framework wiki has a what’s new section with details of the new release.

spring版本升级到5.3。

Java 15 Support

Spring Boot 2.4 now fully supports (and is tested against) Java 15. The minimum supported version remains Java 8.

java支持版本最高15,最低8。

Custom property name support

When using constructor binding the name of the property is derived from the parameter name. This can be a problem if you want to use a java reserved keyword. For such situations, you can now use the @Name annotation, something like:

@ConfigurationProperties(prefix = "sample")
@ConstructorBinding
public class SampleConfigurationProperties {

  private final String importValue;

  public SampleConfigurationProperties(@Name("import") String importValue) {
    this.importValue = importValue;
  }

}

The sample above exposes a sample.import property.

@ConstructorBinding用于在构造函数中注入配置属性,@Name用于给别名字段注入配置属性。

Layered jar enabled by default

This release enables layered jars and include the layertools by default. This should improve the efficiency of generated image using the build pack out-of-the-box and lets you benefit of that feature when crafting custom Dockerfile.

分层JAR是一种将应用程序的依赖项和代码分成不同层的技术,这样可以使得在构建Docker镜像时只重新构建有变化的层,而不是每次都重新构建整个镜像。这可以大大提高Docker镜像的构建速度和效率。
layertools是一个用于创建和管理分层JAR的工具。它可以帮助你创建和管理应用程序的不同层,并确保在构建Docker镜像时只重新构建有变化的层。
总的来说,Spring Boot 2.4的这个更新使得构建和部署Docker镜像更加高效,并且使得自定义Dockerfile更加容易。参考文章Spring Boot官方推荐的Docker镜像编译方式-分层jar包

Docker/Buildpack Support
Publishing Images

The Maven plugin spring-boot:build-image goal and Gradle plugin bootBuildImage task now have the ability to publish the generated image to a Docker registry. See the Maven and Gradle plugin documentation for more details on configuring the plugins for publishing images.

通过此插件可以将生成的镜像直接上传到指定的docker仓库。

Redis Cache Metrics

If you’re using Redis caching you can now expose cache statistics via Micrometer. Metrics logged include the number puts, gets and deletes as well as hits/misses. The number of pending requests and the lock wait duration are also recorded.
To enable the feature, set spring.cache.redis.enable-statistics to true.

spring.cache.redis.enable-statistics=true 开启此配置后,可以在一些监控工具上看到redis缓存的命中、未命中次数等统计信息,看着挺有用的。

Register @WebListeners in a way that allows them to register servlets and filters

Servlet @WebListener classes are now registered in such a way that they may themselves register servlets and filters.
Earlier versions of Spring Boot registered them using a call to javax.servlet.Registration.Dynamic. This meant that the following section of Servlet specification (4.4) applied:
If the ServletContext passed to the ServletContextListener’s contextInitialized method where the ServletContextListener was neither declared in web.xml or web-fragment.xml nor annotated with @WebListener then an UnsupportedOperationException MUST be thrown for all the methods defined in ServletContext for programmatic configuration of servlets, filters and listeners.
As of Spring Boot 2.4, we no longer use dynamic registration and so it’s safe to call event.getServletContext().addServlet(…) and event.getServletContext.addFilter(…) from a ServletContextListener.contextInitialized method.
A side-effect of this change is that the Servlet container now creates the instance of the WebListener and, therefore, dependency injection such as with @Autowired can no longer be used. In such cases, @Component should be used instead.

从Spring Boot 2.4开始,Servlet容器不再直接管理@WebListener注解的类。而是由Spring容器来管理这些类的实例,并通过Servlet容器的API进行注册。因此,在@WebListener类中不能再使用Servlet容器的依赖注入功能,如@Autowired等。如果需要使用Spring的依赖注入功能,可以将该类标记为@Component,并将其放在Spring容器中管理。参考文章Spring Boot @ServletComponentScan 扫描 @WebServlet、@WebFilter(过滤器)、@WebListener(过滤器)

推荐文章

Spring Boot 2.4.0正式发布,全新的配置文件加载机制(不向下兼容) (qq.com)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,710评论 4 376
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,839评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,295评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,776评论 0 223
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,198评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,074评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,200评论 2 322
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,986评论 0 214
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,733评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,877评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,348评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,675评论 3 265
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,393评论 3 246
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,209评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,996评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,212评论 2 287
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 36,003评论 2 280

推荐阅读更多精彩内容