这篇文章主要介绍了Spring Boot启动流程断点过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
启动入口
跟进run方法 : 一个用来使用默认的配置从特定的源运行SpringApplication的静态帮助类。
这个类有两个重载方法,另一个用来传入多个源。通常,单个参数方法是数组方法的一个特例
创建一个新的SpringApplication实例。这个应用程序上下文会从特定的源加载Beans,这个实例会在调用run方法之前被定制化。
Web应用程序类型的枚举:WebApplicationType,包含NONE(不是web应用),SERVLET(基于Servlet的web应用),REACTIVE(基于Reactive的web应用)
- 直接jar包运行不使用web容器
- 使用嵌入式的Servlet web容器
- 使用反应式的web容器
- setInitializers((Collection) getSpringFactoriesInstances(
- ApplicationContextInitializer.class));
用于创建和加载Spring工厂方法实例
4.运行SpringApplication的run方法
Java SPI在 Spring Boot中的应用
SpringBoot底层的自动化都是由这些SPI实现类来实现的:初始化,监听器,自动配置导入监听器,自动配置导入过滤器,自动配置,失败分析器,可用模板提供者
Spring Boot找到main方式的方式
以上都是new SpringBootApplication的过程,下面分析run方法
- /**
- * Run the Spring application,creating and refreshing a new
- * {@link ApplicationContext}.
- * 运行一个Spring应用,创建和刷新一个新的ApplicationContext
- * @param args the application arguments (usually passed from a Java main method)
- * 应用参数通过java main方法传递过来
- * @return a running {@link ApplicationContext}
- */
- public ConfigurableApplicationContext run(String... args) {
- // 任务计时器工具,可同时计数多个任务
- StopWatch stopWatch = new StopWatch();
- stopWatch.start();
- //ApplicationContext是Spring的中心接口,为应用提供配置:1bean工厂2加载资源3注册的监听器发布事件4解析消息
- ConfigurableApplicationContext context = null;
- Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
- //headless 模式:服务器端模式,表示系统没有键盘鼠标等前端应用
- configureHeadlessProperty();
- //监听器容器,对run方法各个阶段事件进行监听,观察者模式
- SpringApplicationRunListeners listeners = getRunListeners(args);
- //监听相应的事件,SpringApplicationEvent下的一个实现
- listeners.starting();
- try {
- //提供了对于运行SpringApplication参数的访问
- ApplicationArguments applicationArguments = new DefaultApplicationArguments(
- args);
- //环境配置:是servlet,reactive或者java应用环境,触发evn准备好的事件
- ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);
- configureIgnoreBeanInfo(environment);
- Banner printedBanner = printBanner(environment);
- context = createApplicationContext();
- exceptionReporters = getSpringFactoriesInstances(
- SpringBootExceptionReporter.class,new Class[] { ConfigurableApplicationContext.class },context);
- prepareContext(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,exceptionReporters,listeners);
- throw new IllegalStateException(ex);
- }
- try {
- listeners.running(context);
- }
- catch (Throwable ex) {
- handleRunFailure(context,null);
- throw new IllegalStateException(ex);
- }
- return context;
- }