- 1. Spring Boot 2.6概述
- 1.1 documentation
- 1.2 SUPPORT
- 1.3 Initializr
- 2. Spring Boot 2.6详解
- 2.1 文档概述
- 2.3 System Requirements
- 2.4 Servlet Containers
- 2.5 POM
- 2.6 SpringBoot2.6 Application
- 2.7 自定义退出码
- 2.8 SpringBoot应用启动器
- 2.9 默认禁止循环引用
- 2.10 重要端点默认关闭
2021年末赶上Spring Boot 2.6.1 CURRENT GA 正式版发布!
1.1 documentationhttps://spring.io/projects/spring-boot#learn
这个是非常重要的一个要素,支持表达的是springboot每个版本的发布时间与终止支持时间,是技术选型的一个重要指标,对于一些不在支持的相对久远的版本使用过程中出现的bug性能等问题不在支持,会给项目带来一定的安全隐患。
从下图中也可以发现值得注意的2.4x的版本截止到2021年底已经终止支持,最终摒弃时间到2023年。
https://spring.io/projects/spring-boot#support
用于快速构建spring相关项目。
Spring 初始化项目地址:https://start.spring.io/
https://docs.spring.io/spring-boot/docs/current/reference/html/
这里简单列举一些说明,因为功能很多就不一一列举了,具体官网文档自行查看:https://docs.spring.io/spring-boot/docs/2.6.1/reference/htmlsingle/
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:
springboot2.6.1支持的容器与servlet。
您还可以将Spring引导应用程序部署到任何与Servlet3.1+兼容的容器中。
Spring Boot supports the following embedded servlet containers:
You can also deploy Spring Boot applications to any servlet 3.1+ compatible container.
2.5 POM2.6 SpringBoot2.6 Application4.0.0 com.example myproject0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-parent2.6.1
@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 自定义退出码
@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引入相关的依赖,和在配置文件中简单的配置就可以使用相应模块了。
订单接口注入商品接口,商品接口注入订单接口。
@Service public class OrderService { private final GoodsService goodsService; public OrderService(GoodsService goodsService) { this.goodsService = goodsService; } } @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:workopencodeboot-projecttargetclassescomzrjbootprojectserviceGoodsService.class] ↑ ↓ | orderService defined in file [D:workopencodeboot-projecttargetclassescomzrjbootprojectserviceOrderService.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 12.10 重要端点默认关闭
Spring Boot Actuator 是 Spring Boot 官方提供的监控模块,提供了很多开箱即用的端点(比如/health、/metrics 、/info)帮助我们来监控和管理 Spring Boot 应用。
pom.xml
org.springframework.boot spring-boot-starter-actuator
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 } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)