SpringBoot?
SpringBoot is starting point for build all spring-based applicaitons.Spring Boot is designed to get you up and running as quickly as possible,with minimal upfront configuration of Spring.
SpringBoot启动过程
1.启动springBoot
public static void main(String[] args) {
SpringApplication.run(SpringbootStudyApplication.class, args);
}
2.创建SpringApplication
new SpringApplication(primarySources)
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.sources = new LinkedHashSet();
this.bannerMode = Mode.CONSOLE;
this.logStartupInfo = true;
this.addCommandLineProperties = true;
this.addConversionService = true;
this.headless = true;
this.registerShutdownHook = true;
this.additionalProfiles = new HashSet();
this.isCustomEnvironment = false;
this.lazyInitialization = false;
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
this.webApplicationType = WebApplicationType.deduceFromClasspath();
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = this.deduceMainApplicationClass();
}
2.1 判断程序类型 (NONO,Servlet,reactor)
this.webApplicationType = WebApplicationType.deduceFromClasspath();
2.2 加载所有的初始化容器
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
2.3 加载所有的监听器
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
2.4 找到程序运行主类
this.mainApplicationClass = this.deduceMainApplicationClass();
3.执行run方法
3.1开启计时器
StopWatch stopWatch = new StopWatch();
stopWatch.start();
3.2配置系统属性java.awt.headless
this.configureHeadlessProperty();
3.3 注册SpringApplicationRunListeners
SpringApplicationRunListeners listeners = this.getRunListeners(args);
(1)获取一个EventPublishingRunListeners的对象
(2)执行starting方法
3.4 加载命令行的参数值
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
3.5 准备当前应用程序的环境
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
this.configureIgnoreBeanInfo(environment);
(1)设置系统属性,保证某些bean不会添加到准备的环境中
private void configureIgnoreBeanInfo(ConfigurableEnvironment environment) {
if (System.getProperty("spring.beaninfo.ignore") == null) {
Boolean ignore = (Boolean)environment.getProperty("spring.beaninfo.ignore", Boolean.class, Boolean.TRUE);
System.setProperty("spring.beaninfo.ignore", ignore.toString());
}
}
3.6 准备banner的打印
Banner printedBanner = printBanner(environment);
3.7 创建上下文应用对象
context = createApplicationContext();
....
case SERVLET:
contextClass = Class.forName("org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext");
break;
3.8设置异常报告器对象
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context)
3.9准备当前上下文对象
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
3.10 刷新上下文环境(关键操作)
refreshContext(context);
3.11 上下文对象准备好之后的操作,默认什么都不做,方便扩展
afterRefresh(context, applicationArguments);
3.12 计时结束,并打印启动程序运行的时长
stopWatch.stop();
3.13 开始所有的监听器对象
listeners.started(context);
3.14
listeners.running(context);