Spring Boot 2.6.1 GA

1. Spring Boot 2.6概述

2021年末赶上Spring Boot 2.6.1 CURRENT GA 正式版发布!

1.1 Documentation

https://spring.io/projects/spring-boot#learn
在这里插入图片描述

1.2 SUPPORT

这个是非常重要的一个要素,支持表达的是springboot每个版本的发布时间与终止支持时间,是技术选型的一个重要指标,对于一些不在支持的相对久远的版本使用过程中出现的bug性能等问题不在支持,会给项目带来一定的安全隐患。
从下图中也可以发现值得注意的2.4x的版本截止到2021年底已经终止支持,最终摒弃时间到2023年。

https://spring.io/projects/spring-boot#support
在这里插入图片描述

1.3 Initializr

用于快速构建spring相关项目。
Spring 初始化项目地址:https://start.spring.io/

2. Spring Boot 2.6详解

2.1 文档概述

https://docs.spring.io/spring-boot/docs/current/reference/html/
在这里插入图片描述
这里简单列举一些说明,因为功能很多就不一一列举了,具体官网文档自行查看:https://docs.spring.io/spring-boot/docs/2.6.1/reference/htmlsingle/

2.3 System Requirements

SpringBoot2.6.1需要Java8,并且与Java17兼容。还需要Spring Framework 5.3.13或更高版本。

Spring Boot 2.6.1 requires Java 8 and is compatible up to and including Java 17. Spring Framework 5.3.13 or above is also required.
Explicit build support is provided for the following build tools:

Build Tool Version
Maven 3.5+
Gradle 6.8.x, 6.9.x, and 7.x

2.4 Servlet Containers

springboot2.6.1支持的容器与servlet。
您还可以将Spring引导应用程序部署到任何与Servlet3.1+兼容的容器中。
Spring Boot supports the following embedded servlet containers:

Containers Servlet Version
Tomcat 9.0 4.0
Jetty 9.4 3.1
Jetty 10.0 4.0
Undertow 2.0 4.0

You can also deploy Spring Boot applications to any servlet 3.1+ compatible container.

2.5 POM

<?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>

    <groupId>com.example</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.1</version>
    </parent>

    <!-- Additional lines to be added here... -->

</project>

2.6 SpringBoot2.6 Application

/**
 * SpringBoot2.6应用测试
 * 启动器和自动配置 @EnableAutoConfiguration
 *
 * @author zrj
 * @since 2021/12/2
 **/
@RestController
@EnableAutoConfiguration
public class BootApplication {
    
    
    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
    
    
        return String.format("Hello %s!", name);
    }

    public static void main(String[] args) {
    
    
        //默认启动方式
        //SpringApplication.run(BootApplication.class, args);

        //自定义启动方式
        SpringApplication springApplication = new SpringApplication(BootApplication.class);
        //自定义Banner,CONSOLE:默认打印控制台,OFF:不打印,LOG:打印到日志文件
        springApplication.setBannerMode(Banner.Mode.CONSOLE);
        //禁止懒加载
        springApplication.setLazyInitialization(false);
        //设置自定义监听器
        //springApplication.addListeners(null);
        //添加自定义的应用初始化器
        springApplication.addInitializers();
        //允许循环引用
        springApplication.setAllowCircularReferences(true);
        //启动应用
        springApplication.run(args);
    }
}

2.7 自定义退出码

/**
 * SpringBoot中自定义退出码
 *
 * @author zrj
 * @since 2021/12/2
 **/
@SpringBootApplication
public class SampleApplication {
    
    
    @Bean
    public ExitCodeGenerator exitCodeGenerator() {
    
    
        System.out.println("应用自定义退出码:42");
        return () -> 42;
    }

    public static void main(String[] args) {
    
    
        System.exit(SpringApplication.exit(SpringApplication.run(SampleApplication.class, args)));
    }
}

2.8 SpringBoot应用启动器

啥是应用启动器?SpringBoot集成了spring的很多模块,比如tomcat、redis等等。你用SpringBoot搭建项目,只需要在pom.xml引入相关的依赖,和在配置文件中简单的配置就可以使用相应模块了。
在这里插入图片描述

2.9 默认禁止循环引用

订单接口注入商品接口,商品接口注入订单接口。

/**
 * 订单接口,循环依赖
 * @author zrj
 * @since 2021/11/26
 **/
@Service
public class OrderService {
    
    
    private final GoodsService goodsService;

    public OrderService(GoodsService goodsService) {
    
    
        this.goodsService = goodsService;
    }
}

/**
 * 商品接口,循环依赖
 *
 * @author zrj
 * @since 2021/11/26
 **/
@Service
public class GoodsService {
    
    
    private final OrderService orderService;

    public GoodsService(OrderService orderService) {
    
    
        this.orderService = orderService;
    }
}
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
┌─────┐
|  goodsService defined in file [D:\work\opencode\boot-project\target\classes\com\zrj\bootproject\service\GoodsService.class]
↑     ↓
|  orderService defined in file [D:\work\opencode\boot-project\target\classes\com\zrj\bootproject\service\OrderService.class]
└─────┘
Action:
Despite circular references being allowed, the dependency cycle between beans could not be broken. Update your application to remove the dependency cycle.

Process finished with exit code 1

2.10 重要端点默认关闭

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

启动应用访问:http://localhost:8080/actuator

1.查看指标参数名称:http://localhost:8080/actuator/metrics
2.替换为指定指标名称,查看监控状况:{requiredMetricName}
http://localhost:8080/actuator/metrics/{requiredMetricName}

{
    
    }_links: {
    
    
  self:   {
    
    
    href:     "http://localhost:8080/actuator",
    templated: false
  },
  metrics:   {
    
    
    href:     "http://localhost:8080/actuator/metrics",
    templated: false
  },
  metrics  -requiredMetricName:   {
    
    
    href:     "http://localhost:8080/actuator/metrics/{requiredMetricName}",
    templated: true
  }
}

在这里插入图片描述在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_37583655/article/details/121678486
GA