springboot(2.4.1)源代码分析二starting

  |   0 评论   |   0 浏览

1598248199146319872.png
上一篇写到springboot的启动步骤,下面看看springboot的启动第一步,starting事件,下面是局部逻辑SpringApplicationRunListeners的starting逻辑,方法入口在org.springframework.boot.SpringApplication类中:

public ConfigurableApplicationContext run(String... args) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		DefaultBootstrapContext bootstrapContext = createBootstrapContext();
		ConfigurableApplicationContext context = null;
		configureHeadlessProperty();
		SpringApplicationRunListeners listeners = getRunListeners(args);
		#【1】starting
		listeners.starting(bootstrapContext, this.mainApplicationClass);
		try {
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
			ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
			configureIgnoreBeanInfo(environment);
			Banner printedBanner = printBanner(environment);
			context = createApplicationContext();
			context.setApplicationStartup(this.applicationStartup);
			prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
			refreshContext(context);
			afterRefresh(context, applicationArguments);
			stopWatch.stop();
			if (this.logStartupInfo) {
				new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
			}
			listeners.started(context);
			callRunners(context, applicationArguments);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, listeners);
			throw new IllegalStateException(ex);
		}

		try {
			listeners.running(context);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, null);
			throw new IllegalStateException(ex);
		}
		return context;
	}

   ......

	void starting(ConfigurableBootstrapContext bootstrapContext, Class<?> mainApplicationClass) {
		doWithListeners("spring.boot.application.starting", (listener) -> listener.starting(bootstrapContext),
				(step) -> {
					if (mainApplicationClass != null) {
						step.tag("mainApplicationClass", mainApplicationClass.getName());
					}
		});
	}

	private void doWithListeners(String stepName, Consumer<SpringApplicationRunListener> listenerAction,Consumer<StartupStep> stepAction) {
		StartupStep step = this.applicationStartup.start(stepName);
		this.listeners.forEach(listenerAction);  //执行listener.starting(bootstrapContext)
		if (stepAction != null) {
			stepAction.accept(step);
		}
		step.end();
	}

  这里采用的函数式编程,我发现springboot里面有很多的函数式编程,这点值得学习,确实代码要简介很多,但是可读性没有非函数式的好,有利有弊吧,毕竟局部逻辑外置了过后,思维有一定的跨度,得来回看代码才清楚具体的逻辑。这里最核心的逻辑其实就是遍历listeners(所有已加载jar包中spring.factories中配置的org.springframework.boot.SpringApplicationRunListener)数组,挨个调用listener.starting(bootstrapContext),这里底层最终会调用org.springframework.context.event.SimpleApplicationEventMulticaster这个类的multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType)方法,event类型是org.springframework.boot.context.event.ApplicationStartingEvent,下面跟进看看这个方法的逻辑:

public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
		ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
		Executor executor = getTaskExecutor();
		for (ApplicationListener<?> listener : getApplicationListeners(event, type)) { //检索有效的listener
			if (executor != null) {
				executor.execute(() -> invokeListener(listener, event)); //如果有executor,则异步通知监听者
			}
			else {
				invokeListener(listener, event);//否则在当前线程挨个调用广播通知监听者
			}
		}
	}

目前跟踪到以下有效starting监听者:

  • org.springframework.boot.context.logging.LoggingApplicationListener:配置spring的日志系统,决定当前应用程序使用哪套日志框架,目前主流的是LogbackLoggingSystem、Log4J2LoggingSystem、JavaLoggingSystem,我这里使用的是log4j2为做日志框架,spring同样是根据classpath下是否存在指定的class去判断系统到底使用的哪个日志框架。
  • org.springframework.boot.context.config.DelegatingApplicationListener:这个是一个委派类,将ApplicationStartingEvent事件直接转发给context.listener.classes这个环境变量配置的监听者,通过这个配置,可以定义自己的系统启动监听器。
  • org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener:底层有个JNI接口com.sun.jndi.ldap.ServiceLocator设置CustomResolverServiceLocator的逻辑,大概就是要找什么服务就直接在spring的扫描逻辑中去找即可,没用过liquibase,感兴趣的自行百度。

标题:springboot(2.4.1)源代码分析二starting
作者:michael
地址:https://blog.junxworks.cn/articles/2021/06/23/1624443160847.html