springboot启动源码 - registerListeners
Posted Ledison7
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot启动源码 - registerListeners相关的知识,希望对你有一定的参考价值。
registerListeners 源码解析
Check for listener beans and register them.
监听器注册
自定义监听事件
package org.hopehomi.core.tool.support.listener;
import org.springframework.context.ApplicationEvent;
/**
* @author Ledison
* @date 2023/3/22
*/
public class ForDebuggerEvent extends ApplicationEvent
public ForDebuggerEvent(Object source)
super(source);
public void executeEvent()
System.out.println("ForDebuggerEvent executeEvent");
自定义事件监听器
package org.hopehomi.core.tool.support.listener;
import org.springframework.context.ApplicationListener;
/**
*
* @author Ledison
* @date 2023/3/22
*/
public class ForDebuggerEventListener implements ApplicationListener<ForDebuggerEvent>
@Override
public void onApplicationEvent(ForDebuggerEvent event)
event.executeEvent();
自定义事件生产类
package org.hopehomi.core.tool.support.listener;
import cn.hutool.extra.spring.SpringUtil;
import java.util.HashMap;
/**
* @author Ledison
* @date 2023/3/22
*/
public class ForDebuggerEventPusher
public static void pushEvent()
SpringUtil.publishEvent(new ForDebuggerEvent(new HashMap<>()));
ApplicationListener实现类什么时候被加载并实例化的
在SpringApplication的构造方法中
getSpringFactoriesInstances方法
依然是通过SpringFactoriesLoader.loadFactoryNames(Class<?> factoryType, ClassLoader classLoader),查找spring.factories下key为interface org.springframework.context.ApplicationListener的类名。
获取到类名后,通过构造器对这些类进行实例化
返回实例集合后,再将这些对象放入SpringApplication中
当SpringApplication开始调用run方法,在创建context之后的prepareContext方法中,会将SpringApplication的listeners设置到contextapplicationListeners中
prepareContext方法
contextLoaded方法
补充说明,可以看到这里有很多类似的方法,他们都是根据SpringApplicationRunListener的回调时机来触发
doWithListeners方法
补充说明一下,这里的listeners是基于spring SPI查询到的key为org.springframework.boot.SpringApplicationRunListener的实现类
遍历SpringApplicationRunListener,并通过Consumer执行listener.contextLoaded(context)
调用EventPublishingRunListener重写方法contextLoaded,并将SpringApplication的listeners设置到context的applicationListeners中
进入正题registerListeners
- 将context的applicationListeners,注册到事件控制器(context的ApplicationEventMulticaster applicationEventMulticaster,默认实现类是SimpleApplicationEventMulticaster)的内部类(DefaultListenerRetriever)的Set<ApplicationListener<?>> applicationListeners中
Register statically specified listeners first.
通过spring spi 加载org.springframework.context.ApplicationListener
- 通过beanFactory的beanDefinitionNames获取类型为ApplicationListener的bean名称集合,注册到事件控制器的applicationListenerBeans中
Do not initialize FactoryBeans here: We need to leave all regular beans
uninitialized to let post-processors apply to them!
通过spring spi 加载org.springframework.boot.autoconfigure.EnableAutoConfiguration
3. 加载earlyApplicationEvents到事件控制器,暂时不分析
Publish early application events now that we finally have a multicaster…
总结
- 通过spring spi 加载ApplicationListener的类,实例化后注入到context的applicationEventMulticaster中,DefaultListenerRetriever的applicationListeners集合
- 通过spring spi 加载EnableAutoConfiguration的类,将beanName注入到context的applicationEventMulticaster中,DefaultListenerRetriever的applicationListenerBeans集合
SpringBoot源码学习系列之启动原理简介
本博客通过debug方式简单跟一下Springboot application启动的源码,Springboot的启动源码是比较复杂的,本博客只是简单梳理一下源码,浅析其原理
为了方便跟源码,先找个Application类,打个断点,进行调试,如图所示:
step into,run方法调用了SpringApplication的run方法
通过debug,Springboot启动过程,会先执行如下关键的构造函数
分析构造函数源码:
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
// 判断当前的web类型
this.webApplicationType = WebApplicationType.deduceFromClasspath();
//设置初始化的ApplicationInitializer类,从类路径下面的META-INF/spring.factories配置文件获取所有的ApplicationInitializer保存起来
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
//同理,从类路径下面的META-INF/spring.factories配置文件获取所有的ApplicationListener保存起来
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
//从多个配置类中找到有main方法的主配置类
this.mainApplicationClass = deduceMainApplicationClass();
}
注意:上面过程其实就是创建Springboot的Application启动类的过程
deduceFromClasspath方法是判断web类型的
继续debug ApplicationContextInitializer这些Initializer类,可以说是初始化类的设置过程
SpringFactoriesLoader.loadFactoryNames(type, classLoader)获取所有的Initializer类的类名
Evaluate可以看出扫描到如下的类
继续debug,这个是Spring框架的底层类
找到主要的源码,loadSpringFactories方法也是从如下的位置获取配置信息的
从META-INF/spring.factories获取对应的配置信息
框架的文件位置在autoconfiguration工程里,显然如果要自定义Initializer类的话,自己新建一些Initializer类,然后自己写个META-INF/spring.factories类,也是可以被扫描到的
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
//用一个ConcurrentReferenceHashMap来缓存信息
MultiValueMap<String, String> result = cache.get(classLoader);
if (result != null) { //缓存读取到配置信息,返回缓存数据
return result;
}
// 缓存读取不到的情况,重新从META-INF/spring.factories配置文件读取
try {
Enumeration<URL> urls = (classLoader != null ?
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
result = new LinkedMultiValueMap<>();
// 遍历循环读取配置信息
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
UrlResource resource = new UrlResource(url);
// 用PropertiesLoaderUtils工具类读取资源文件
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
for (Map.Entry<?, ?> entry : properties.entrySet()) {
//获取到Initializer对应的全类名
String factoryTypeName = ((String) entry.getKey()).trim();
for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
result.add(factoryTypeName, factoryImplementationName.trim());
}
}
}
// 重新放在缓存里
cache.put(classLoader, result);
return result;
}
catch (IOException ex) {
throw new IllegalArgumentException("Unable to load factories from location [" +
FACTORIES_RESOURCE_LOCATION + "]", ex);
}
}
ApplicationInitializer类的全类名都被扫描到之后,返回刚才的源码,继续看看,如图,从命名看应该是进行类的实例化过程
step into,果然是的,还是调用了Spring框架的底层工具类,BeanUtils进行类的实例化过程
setListeners方法的过程同理,本文就不详细分析:
继续往下debug,deduceMainApplicationClass方法
private Class<?> deduceMainApplicationClass() {
try {
//获取运行时的堆栈属性数组
StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
//有main方法的Application类返回
if ("main".equals(stackTraceElement.getMethodName())) {
return Class.forName(stackTraceElement.getClassName());
}
}
}
catch (ClassNotFoundException ex) {
// Swallow and continue
}
return null;
}
获取到的就是创建Springboot工程时的Application类
Springboot的Application类创建成功之后,才真正开始执行run方法
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
//校验java.awt.headless的
configureHeadlessProperty();
//从META-INF/spring.factories获取SpringApplicationRunListeners,和前面的分析同理,本文就不详细介绍
SpringApplicationRunListeners listeners = getRunListeners(args);
//回调SpringApplicationRunListeners 的starting方法
listeners.starting();
try {
//封装命令行参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//准备环境,环境创建完成之后,再回调SpringApplicationRunListeners 的environmentPrepared方法,表示环境准备好
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
configureIgnoreBeanInfo(environment);
//控制台打印Banner信息的,后面再简单分析
Banner printedBanner = printBanner(environment);
// 创建Spring的IOC容器,创建过程比较复杂,会分析是web类型的ioc容器,还是普通的ioc容器等等
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
//将environment保存到ioc,执行applyInitializers方法,applyInitializers方法执行完成之后,再回调SpringApplicationRunListeners的contextPrepared方法
//applyInitializers方法作用:回调之前保存的所有的ApplicationContextInitializer的initialize方法
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
//刷新ioc容器,其实就是ioc容器的初始化过程,还没进行属性设置,后置处理器,仅仅是扫描、创建、加载所有组件等等过程
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
//回调所有SpringApplicationRunListener的started方法
listeners.started(context);
//从ioc容器中获取所有的ApplicationRunner和CommandLineRunner进行回调,ApplicationRunner先回调,CommandLineRunner再回调
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
//回调所有SpringApplicationRunListener的running方法
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
//Springboot应用启动成功后,才返回启动的ioc容器
return context;
}
回顾一下前面源码的环境准备方法,找重点代码,如图,可以看出环境准备完成后会回调SpringApplicationRunListener的environmentPrepared方法,表示环境准备完成
banner打印的方法,如图,执行完成,控制台的banner信息就打印出来了:
ioc初始化之前,会执行applyInitializers方法,执行完成后,再回调SpringApplicationRunListener的contextPrepared方法
applyInitializers():回调之前保存的所有的ApplicationContextInitializer的initialize方法
从ioc容器中获取所有的ApplicationRunner和CommandLineRunner进行回调
ok,从源码的简单分析,可以看出有几个重要的事件监听机制,下面引用尚硅谷视频的例子:
只需要放在ioc容器中的有:
- ApplicationRunner
@Component
public class HelloApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner...run....");
}
}
- CommandLineRunner
@Component
public class HelloCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("CommandLineRunner...run..."+ Arrays.asList(args));
}
}
配置在META-INF/spring.factories的有:
- ApplicationContextInitializer
public class HelloApplicationContextInitializer implements
ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
System.out.println("ApplicationContextInitializer...initialize..."+applicationContext);
}
}
- SpringApplicationRunListener
package com.example.springboot.web.listener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
public class HelloSpringApplicationRunListener implements SpringApplicationRunListener {
//必须有的构造器
public HelloSpringApplicationRunListener(SpringApplication application, String[] args){
}
@Override
public void starting() {
System.out.println("SpringApplicationRunListener...starting...");
}
@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
Object o = environment.getSystemProperties().get("os.name");
System.out.println("SpringApplicationRunListener...environmentPrepared.."+o);
}
@Override
public void contextPrepared(ConfigurableApplicationContext context) {
System.out.println("SpringApplicationRunListener...contextPrepared...");
}
@Override
public void contextLoaded(ConfigurableApplicationContext context) {
System.out.println("SpringApplicationRunListener...contextLoaded...");
}
}
配置(META-INF/spring.factories)
org.springframework.context.ApplicationContextInitializer=com.example.springboot.web.listener.HelloApplicationContextInitializer
org.springframework.boot.SpringApplicationRunListener=com.example.springboot.web.listener.HelloSpringApplicationRunListener
例子下载:github下载链接
以上是关于springboot启动源码 - registerListeners的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot源码分析之---SpringBoot项目启动类SpringApplication浅析