Spring boot在不特别配置的情况下默认使用tomcat作为启动容器,下面介绍一下我们的分析思路
spring boot的启动过程源码很繁琐,但是大致我们可以总结一下分为三个阶段:
1.初始化配置工厂
2.获取Servlet容器并启动
阶段一:初始化配置工厂
1.调试跟踪
那么我们又是如何分析到的呢,首先我们通过调试如下:
通过调试可以跟踪到是通过invokeBeanFactoryPostProcessors(beanFactory)方法将tomcat的工厂定义配置进去的
从这里解析的配置,函数名processConfigBeanDefinitions(BeanDefinitionRegistry registry)
我们对beanDefinition的注册方法加条件断点即可调试到tomcat的Factory方法是何时注册的
2.源码分析
下面进入我们的主流程:
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerMain {
public static void main(String[] args) {
SpringApplication.run(EurekaServerMain.class, args);
}
}
//SpringApplication.java
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
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);//