22.05.29--22.06.12

SpringBoot的启动流程

启动源码

	public ConfigurableApplicationContext run(String... args) {
		long startTime = System.nanoTime();
		DefaultBootstrapContext bootstrapContext = createBootstrapContext();
		ConfigurableApplicationContext context = null;
		configureHeadlessProperty();
		SpringApplicationRunListeners listeners = getRunListeners(args);
		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);
			Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
			if (this.logStartupInfo) {
				new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), timeTakenToStartup);
			}
			listeners.started(context, timeTakenToStartup);
			callRunners(context, applicationArguments);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, listeners);
			throw new IllegalStateException(ex);
		}
		try {
			Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
			listeners.ready(context, timeTakenToReady);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, null);
			throw new IllegalStateException(ex);
		}
		return context;
	}

参考链接:Springboot启动流程_智由静生的博客-CSDN博客_springboot启动流程

SpringBoot如何内嵌Tomcat

导入starter-web依赖的时候会默认导入spring-boot-starter-tomcat,这是Tomcat启动的相关环境

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

启动类上的@SpringBootApplication主要实现Spring组件扫描和自动配置。其中包含两个重要注解:@SpringBootConfiguration和@EnableAutoConfiguration。

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@EnableAutoConfiguration本身是一个复合注解,这两个注解比较关键。@AutoConfigurationPackage主要实现自动配置包,会扫描@SpringbootApplication标注的类所在包名及其子包,将创建的组件添加到容器中;而@Import则是导入了AutoConfigurationImportSelector.class,实现查找classpath上所有jar包中的META-INF/spring.factories,找出其中的自动配置类并导入到容器中,其中Web容器所对应的自动配置类为ServletWebServerFactoryAutoConfiguration。

@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
}

ServletWebServerFactoryAutoConfiguration中支持好几种web容器,比如Tomcat、Jetty和Undertow。

而EmbeddedTomcat则是Tomcat组件相关的类,本身是一个FactoryBean,用来实例化TomcatServletWebServerFactory。此时TomcatServletWebServerFactory中就包含了创建和启动Tomcat的方法getWebServer()。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值