Spring Boot 2.6 重磅发布!!!

等了很久, Spring Boot 2.6 终于在昨天发布啦!

1ce1e840b407687980866707d75565c5.png

我还是今天无聊逛推特的时候看到自己关注的一个 Spring 项目的一个开发大佬发的动态才知道的。看来多关注一些大佬了解技术最新动向还是很有必要的!

2ed1caf0ea1fd1246d1fae858ffd78d1.png

Spring Boot 2.6 重要改动

这次更新为我们带来很多好用的新特性/改进,我这里简单总结一下。

详细的 Spring Boot 2.6 改动信息,小伙伴们可以在这里找到:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.6-Release-Notes 。你可以结合 Spring Boot 2.6 的官方文档来学习,地址:https://docs.spring.io/spring-boot/docs/current/reference/html/index.html 。

info 端点暴露 Java 运行信息

Spring Boot Actuator 是 Spring Boot 官方提供的监控模块,提供了很多开箱即用的端点(比如/health/metrics/info)帮助我们来监控和管理 Spring Boot 应用。

几行代码,我们就可以为 Spring Boot 项目引入 Spring Boot Actuator 。

pom.xml

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

application.properties

# 暴露 info 端点
management.endpoints.web.exposure.include=info
management.info.java.enabled=true

Spring Boot 2.6 版本对 info 端点进行了增强改进:

1eba5fbd81f9e29b409ec33db858260f.png

metrics 端点暴露启动和磁盘空间信息

application.properties

management.endpoints.web.exposure.include=metrics

metrics 端点分别新增了 2 个应用程序启动和磁盘空间信息的指标。

ef82ecfc0c4a5713925c65ab90fbdcf2.png

获取某个具体的指标的详细信息: http://localhost:8080/actuator/metrics/{MetricName}

ddf3615b7792dea7b739491893d626e6.png

默认情况下禁止循环引用

如果两个类互相引用对方,那就会发生循环依赖问题。

@Service
public class OrderService {
    @Autowired
    private UserService userService;
}
@Service
public class UserService {
    @Autowired
    private OrderService orderService;
}

在 Spring Boot 2.6 版本中,循环引用默认情况下已经被禁止了。如果你的项目存在循环引用问题,那你在启动项目的时候就会报错。

65f6cbd654ee85edbb7aa80c0b1df39f.png

如果你想关闭禁止循环引用的话,可以直接修改配置 spring.main.allow-circular-referencestrue

WebTestClient 支持测试 Spring MVC

WebTestClient 诞生之初主要是为了测试 Spring WebFlux 项目,这次改版之后,WebTestClient 已经可以支持普通的 Spring MVC 项目。

@SpringBootTest
@AutoConfigureWebTestClient
class MyMockWebTestClientTests {
    @Autowired
    WebTestClient webClient;

    @Test
    void helloWorldTest() {
        webClient
                .get().uri("/hello")
                .exchange()
                .expectStatus().isOk()
                .expectBody(String.class).isEqualTo("Hello World!");
    }
}

Docker 镜像构建功能增强

Spring Boot 官方提供了 docker-maven-plugin 插件来帮助我们构建 Docker 镜像。

在 Spring Boot 2.6 版本中,这个插件的功能得到了小幅加强:

  • 支持自定义镜像设置额外的 Tags

  • 网络配置

  • 构建缓存配置

Maven 构建信息的默认时间

Maven 插件在构建项目的时候可以使用 project.build.outputTimestamp属性值作为默认构建时间。

自动启用 Redis 连接池

你的项目如果依赖了 commons-pool2 的话,Redis(Jedis 和 Lettuce)将会自动启动连接池。

如果你想禁用自动启用连接池,可以设置spring.redis.jedis.pool.enabledspring.redis.lettuce.pool.enabledfalse

总结

a7090d9cc475b4f78fb9beb32c2a6081.png

相比于上一个版本,Spring Boot 2.6 的改进还是比较多的,不过都是比较小的改动和升级,简单了解和学习一下就好。没有太大必要升级,带来收益比较小。

 
  

— 【 THE END 】—

本公众号全部博文已整理成一个目录,请在公众号里回复「m」获取!

最近面试BAT,整理一份面试资料《Java面试BATJ通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
获取方式:点“在看”,关注公众号并回复 PDF 领取,更多内容陆续奉上。
文章有帮助的话,在看,转发吧。
谢谢支持哟 (*^__^*)

猜你喜欢

转载自blog.csdn.net/a724888/article/details/121571479