1. SpringBoot 高级 原理分析 – 监听机制
1.1 引入
SpringBoot 的监听机制,其实是对Java提供的事件监听机制的封装。
Java中的事件监听机制定义了以下几个角色:
- ① 事件:Event,继承 java.util.EventObject 类的对象
- ② 事件源:Source ,任意对象Object
- ③ 监听器:Listener,实现 java.util.EventListener 接口 的对象
SpringBoot 在项目启动时,会对几个监听器进行回调,我们可以实现这些监听器接口,在项目启动时完成一些操作。
- ApplicationContextInitializer
- SpringApplicationRunListener
- CommandLineRunner
- ApplicationRunner
准备工作 新建一个SpringBoot工程模块:
1.2 新建4个自定义的监听器(实现上面4个监听器接口)
MyApplicationContextInitializer.java
package com.tian.listener;import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;@Component
public class MyApplicationContextInitializer implements ApplicationContextInitializer {@Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {System.out.println("ApplicationContextInitializer....initialize");}
}
MyApplicationRunner.java
package com.tian.listener;import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;import java.util.Arrays;
@Component
public class MyApplicationRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("ApplicationRunner...run");System.out.println(Arrays.asList(args.getSourceArgs()));}
}
MyCommandLineRunner.java
package com.tian.listener;import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;import java.util.Arrays;@Component
public class MyCommandLineRunner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("CommandLineRunner...run");System.out.println(Arrays.asList(args));}
}
MySpringApplicationRunListener.java
package com.tian.listener;import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.stereotype.Component;public class MySpringApplicationRunListener implements SpringApplicationRunListener {public MySpringApplicationRunListener(SpringApplication application, String[] args) {}@Overridepublic void starting() {System.out.println("starting...项目启动中");}@Overridepublic void environmentPrepared(ConfigurableEnvironment environment) {System.out.println("environmentPrepared...环境对象开始准备");}@Overridepublic void contextPrepared(ConfigurableApplicationContext context) {System.out.println("contextPrepared...上下文对象开始准备");}@Overridepublic void contextLoaded(ConfigurableApplicationContext context) {System.out.println("contextLoaded...上下文对象开始加载");}@Overridepublic void started(ConfigurableApplicationContext context) {System.out.println("started...上下文对象加载完成");}@Overridepublic void running(ConfigurableApplicationContext context) {System.out.println("running...项目启动完成,开始运行");}@Overridepublic void failed(ConfigurableApplicationContext context, Throwable exception) {System.out.println("failed...项目启动失败");}
}
1.3 启动项目测试
传递参数 再次测试:
1.4 结论
ApplicationRunner和CommandLineRunner一般我们只使用其中一个,他们的功能都是类型的。
1.5 配置ApplicationContextInitializer
和SpringApplicationRunListener
新建资源配置文件
这个文件在SpringBoot工程启动时会去自动扫描配置信息,这是一个固定的写法
org.springframework.context.ApplicationContextInitializer=\com.tian.listener.MyApplicationContextInitializerorg.springframework.boot.SpringApplicationRunListener=\com.tian.listener.MySpringApplicationRunListener
运行结果:
1.6 分析SpringBoot源码