读书笔记 -- Spring Boot3 核心技术 Chapter4 Spring Boot 启动过程与扩展应用
1.读书笔记 -- Spring Boot3 核心技术 Chapter1 Spring Boot 基础入门2.读书笔记 -- Spring Boot3 核心技术 Chapter2 Spring Boot 配置管理3.读书笔记 -- Spring Boot3 核心技术 Chapter5 Spring Boot 日志管理
4.读书笔记 -- Spring Boot3 核心技术 Chapter4 Spring Boot 启动过程与扩展应用
5.读书笔记 -- Spring Boot3 核心技术 Chapter3 Spring Boot Starter 与自动配置4.1 启动入口
1. 应用启动类
@SpringBootApplication:用于标注Spring Boot的启动类。它是@SpringBootApplication注解的一个派生注解,用于告诉Spring Boot这是一个Spring Boot应用程序的入口点
包括:
- @SpringBootConfiguration
- @EnableAutoConfiguration
- @ComponontScan
一般建议启动入口类放置在根目录下,以便使用注解扫描并管理所有子目录包的组件。
2. 应用启动方法
// 1. SpringApplication 直接调用 run() SpringApplication.run(Application.class); // 2. SpringApplication 创建对象并调用 run() SpringApplicaiton springApplication = new SpringApplication(Application.class); // 自定义设置 springApplication.xxx(); springApplication.run(args); // 3. SpringApplicationBuilder new SpringApplicationBuilder() .sources(Parent.class) .child(Application.class) .bannerMode(Banner.Mode.OFF) .run(args);
4.3 启动失败分析 FailureAnalyzer
1. 重写 FailureAnalyzer
测试重写 PortInUseFailureAnalyzer
1) \analyzer\PortInUseFailureAnalyzer:Override 覆盖
2) \resources\META-INF\spring.factories,注册 PortInUseFailureAnalyzer
2. 自定义 FailureAnalyzer
1) analyzer\JavastackFailureAnalyzer
2) spring.factories,注册该 FailureAnalyzer
*** 备注:自定义的 FailureAnalyzer 需要注册在 spring.factories 里。如果放在 新的自动配置类注册文件中(....\*.imports)也不会生效。
4.5 启动图案
1. 关闭 Banner
// 方式1:启动类中关闭 springApplication.setBannerMode(Banner.Mode.OFF); // 方式2:配置文件中通过参数关闭(如 application.yml) spring: main: banner-mode: off
2. 自定义图案
在 resource\ 下,创建一个 banner.txt,如
____. __ __ | |____ ___ _______ _______/ |______ ____ | | __ | \__ \\ \/ /\__ \ / ___/\ __\__ \ _/ ___\| |/ / /\__| |/ __ \\ / / __ \_\___ \ | | / __ \\ \___| < \________(____ /\_/ (____ /____ > |__| (____ /\___ >__|_ \ \/ \/ \/ \/ \/ \/ Spring Boot Version: ${spring-boot.version}
4.6 启动事件和监听器
1. 启动事件的顺序
Spring Boot 启动后,事件的顺序是:
- ApplicationStartingEvent
- ApplicationEnvironmentPreparedEvent
- ApplicationContextInitializedEvent
- ApplicationPreparedEvent
- ApplicationStartedEvent
- AvailabilityChangeEvent
- ApplicationReadyEvent
- AvailabilityChangeEvent
- ApplicationFailedEvent
- WebServerInitializedEvent
- ContextRefreshedEvent
合集:
SprintBoot3核心技术
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)