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
andapplication.yml
files are processed. If you only have a simpleapplication.properties
orapplication.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 aspring.config.use-legacy-processing
property totrue
in yourapplication.properties
orapplication.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 pluginbootBuildImage
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(过滤器)。