● Java17
● Spring、SpringMVC、MyBatis
● Maven、IDEA
环境&工具
版本(or later)
SpringBoot
3.1.x
IDEA
2023.x
Java
17+
Maven
3.5+
Tomcat
10.0+
Servlet
5.0+
GraalVM Community
22.3+
Native Build Tools
0.9.19+
SpringApplicationRunListener
来监听事件;SpringApplicationRunListener
实现类META-INF/spring.factories
中配置 org.springframework.boot.SpringApplicationRunListener=自己的Listener
,还可以指定一个 有参构造器 ,接受两个参数(SpringApplication application, String[] args)
spring-boot.jar
中配置了默认的 Listener,如下/**
* Listener先要从 META-INF/spring.factories 读到
*
* 1、引导: 利用 BootstrapContext 引导整个项目启动
* starting: 应用开始,SpringApplication的run方法一调用,只要有了 BootstrapContext 就执行
* environmentPrepared: 环境准备好(把启动参数等绑定到环境变量中),但是ioc还没有创建;【调一次】
* 2、启动:
* contextPrepared: ioc容器创建并准备好,但是sources(主配置类)没加载。并关闭引导上下文;组件都没创建 【调一次】
* contextLoaded: ioc容器加载。主配置类加载进去了。但是ioc容器还没刷新(我们的bean没创建)。
* =======截止以前,ioc容器里面还没造bean呢=======
* started: ioc容器刷新了(所有bean造好了),但是 runner 没调用。
* ready: ioc容器刷新了(所有bean造好了),所有 runner 调用完了。
* 3、运行
* 以前步骤都正确执行,代表容器running。
*/
BootstrapRegistryInitializer
: 感知特定阶段:感知引导初始化
META-INF/spring.factories
bootstrapContext
的时候触发。addBootstrapRegistryInitializer
();进行密钥校对授权。
ApplicationContextInitializer: 感知特定阶段: 感知ioc容器初始化
META-INF/spring.factories
ApplicationListener: 感知全阶段:基于事件机制,感知事件。 一旦到了哪个阶段可以做别的事
@Bean
或@EventListener
: 事件驱动
SpringApplication.addListeners(…)
或 SpringApplicationBuilder.listeners(…)
META-INF/spring.factories
SpringApplicationRunListener: 感知全阶段生命周期 + 各种阶段都能自定义操作; 功能更完善。
META-INF/spring.factories
ApplicationRunner: 感知特定阶段:感知应用就绪Ready。卡死应用,就不会就绪
@Bean
CommandLineRunner: 感知特定阶段:感知应用就绪Ready。卡死应用,就不会就绪
@Bean
最佳实战:
BootstrapRegistryInitializer
和 ApplicationContextInitializer
**ApplicationRunner**
和 **CommandLineRunner**
**SpringApplicationRunListener**
**ApplicationListener**
9大事件
触发顺序&时机
ApplicationStartingEvent
:应用启动但未做任何事情, 除过注册listeners and initializers.ApplicationEnvironmentPreparedEvent
: Environment 准备好,但context 未创建.ApplicationContextInitializedEvent
: ApplicationContext 准备好,ApplicationContextInitializers 调用,但是任何bean未加载ApplicationPreparedEvent
: 容器刷新之前,bean定义信息加载ApplicationStartedEvent
: 容器刷新完成, runner未调用=以下就开始插入了探针机制====
AvailabilityChangeEvent
: LivenessState.CORRECT
应用存活; 存活探针ApplicationReadyEvent
: 任何runner被调用AvailabilityChangeEvent
:ReadinessState.ACCEPTING_TRAFFIC
就绪探针,可以接请求ApplicationFailedEvent
:启动出错应用事件发送顺序如下:
感知应用是否存活了:可能植物状态,虽然活着但是不能处理请求。
应用是否就绪了:能响应请求,说明确实活的比较好。
应用启动过程生命周期事件感知(9大事件)、应用运行中事件感知(无数种)。
ApplicationEventPublisherAware
或注入:ApplicationEventMulticaster
组件 + @EventListener
事件发布者
@Service
public class EventPublisher implements ApplicationEventPublisherAware {
/**
* 底层发送事件用的组件,SpringBoot会通过ApplicationEventPublisherAware接口自动注入给我们
* 事件是广播出去的。所有监听这个事件的监听器都可以收到
*/
ApplicationEventPublisher applicationEventPublisher;
/**
* 所有事件都可以发
* @param event
*/
public void sendEvent(ApplicationEvent event) {
//调用底层API发送事件
applicationEventPublisher.publishEvent(event);
}
/**
* 会被自动调用,把真正发事件的底层组组件给我们注入进来
* @param applicationEventPublisher event publisher to be used by this object
*/
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
}
事件订阅者
@Service
public class CouponService {
@Order(1)
@EventListener
public void onEvent(LoginSuccessEvent loginSuccessEvent){
System.out.println("===== CouponService ====感知到事件"+loginSuccessEvent);
UserEntity source = (UserEntity) loginSuccessEvent.getSource();
sendCoupon(source.getUsername());
}
public void sendCoupon(String username){
System.out.println(username + " 随机得到了一张优惠券");
}
}
应用关注的三大核心:场景、配置、组件
导入starter
依赖导入autoconfigure
寻找类路径下 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
文件
启动,加载所有 自动配置类
xxxAutoConfiguration
效果:
SPI的思想
是,定义一个接口或抽象类,然后通过在classpath中定义实现该接口的类来实现对组件的动态发现和加载。META-INF/services
目录下创建一个以服务接口全限定名为名字的文件,文件中包含实现该服务接口的类的全限定名。当应用程序启动时,Java的SPI机制会自动扫描classpath中的这些文件,并根据文件中指定的类名来加载实现类。以上回答来自ChatGPT-3.5
在SpringBoot中,META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
作业:写一段java的spi机制代码
自动配置:全部都配置好,什么都不用管。 自动批量导入
* 项目一启动,spi文件中指定的所有都加载。
@EnableXxxx
:手动控制哪些功能的开启; 手动导入。
* 开启xxx功能
就是: @Configuration ,容器中的组件,配置类。spring ioc启动就会加载创建这个类对象
开启自动配置
@Import(AutoConfigurationPackages.Registrar.class)
想要给容器中导入组件。List<String> configurations = ImportCandidates.load(AutoConfiguration.class, getBeanClassLoader())
.getCandidates();
扫描SPI文件:
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
组件扫描:排除一些组件(哪些不要)
排除前面已经扫描进来的
配置类
、和自动配置类
。
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
生命周期启动加载流程
场景:抽取聊天机器人场景,它可以打招呼。
效果:任何项目导入此starter
都具有打招呼功能,并且问候语中的人名需要可以在配置文件中修改
自定义starter
项目,引入spring-boot-starter
基础依赖xxxAutoConfiguration
自动配置类,帮其他项目导入这个模块需要的所有组件META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
指定启动需要加载的自动配置自定义配置有提示。导入以下依赖重启项目,再写配置文件就有提示
@ConfigurationProperties(prefix = "robot") //此属性类和配置文件指定前缀绑定
@Component
@Data
public class RobotProperties {
private String name;
private String age;
private String email;
}
@ConfigurationProperties(prefix = "robot") //此属性类和配置文件指定前缀绑定
@Component
@Data
public class RobotProperties {
private String name;
private String age;
private String email;
}
<!-- 导入配置处理器,配置文件自定义的properties配置都会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency><!-- 导入配置处理器,配置文件自定义的properties配置都会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
创建starter项目,把公共代码需要的所有依赖导入
把公共代码复制进来
自己写一个 RobotAutoConfiguration
,给容器中导入这个场景需要的所有组件
* 为什么这些组件默认不会扫描进去?
别人引用这个starter
,直接导入这个 RobotAutoConfiguration
,就能把这个场景的组件导入进来
功能生效。
测试编写配置文件
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import(RobotAutoConfiguration.class)
public @interface EnableRobot {
}@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import(RobotAutoConfiguration.class)
public @interface EnableRobot {
}
别人引入starter
需要使用 @EnableRobot
开启功能
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 文件中编写好我们自动配置类的全类名即可
项目启动,自动加载我们的自动配置类
手机扫一扫
移动阅读更方便
你可能感兴趣的文章