SpringApplication.run(DemoApplication.class, args); ->
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) { return run(new Class[]{primarySource}, args); // -> }
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) { return (new SpringApplication(primarySources)).run(args); //初始化SpringApplication,并运行 -> }
//初始化SpringApplication
public SpringApplication(Class<?>... primarySources) { this((ResourceLoader)null, primarySources); ->//设置resourceLoader为null, primarySources 为启动类DemoApplication.class }
//todo 标明每个属性的作用 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; //设置java.swt.headless的值 this.registerShutdownHook = true; this.additionalProfiles = Collections.emptySet(); this.isCustomEnvironment = false; this.lazyInitialization = false; this.applicationContextFactory = ApplicationContextFactory.DEFAULT; //设置上下文工厂,默认使用默认方式 this.applicationStartup = ApplicationStartup.DEFAULT; this.resourceLoader = resourceLoader; Assert.notNull(primarySources, "PrimarySources must not be null"); this.primarySources = new LinkedHashSet(Arrays.asList(primarySources)); this.webApplicationType = WebApplicationType.deduceFromClasspath(); //设置类类型 this.bootstrapRegistryInitializers = this.getBootstrapRegistryInitializersFromSpringFactories(); this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class)); this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class)); this.mainApplicationClass = this.deduceMainApplicationClass();//根据堆栈记录获取运行类 }
//运行初始化的类 public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); //获取每个任务的运行时间状况 stopWatch.start(); //设置当前任务名称和启动时间 DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();//加载启动上下文 这里第一次执行,注册表初始器为空 ConfigurableApplicationContext context = null; //配置应用程序上下文,还没有实现 this.configureHeadlessProperty(); //设置java.awt.headless ,默认为true. java.awt.headless是J2SE的一种模式用于在缺少显示屏、键盘或者鼠标时的系统配置,很多监控工具如jconsole 需要将该值设置为true. SpringApplicationRunListeners listeners = this.getRunListeners(args);//设置运行监听器 完成类实例的创建 ->
listeners.starting(bootstrapContext, this.mainApplicationClass);//启动程序
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); //管理应用程序参数
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments); //应用程序环境 ->
this.configureIgnoreBeanInfo(environment); //根据环境配置属性:spring.beaninfo.ignore
Banner printedBanner = this.printBanner(environment); //打印banner信息
context = this.createApplicationContext(); //创建应用程序上下文 ->
context.setApplicationStartup(this.applicationStartup); //为应用程序上下文设置应用程序启动
this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner); //准备上下文 ->
this.refreshContext(context); //刷新上下文 ->
this.afterRefresh(context, applicationArguments); //刷新之后 默认为空方法,可拓展
stopWatch.stop(); //关闭运行监听器
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
listeners.started(context); //监听器启动应用程序上下文
this.callRunners(context, applicationArguments);//运行器运行程序
} catch (Throwable var10) {
this.handleRunFailure(context, var10, listeners);
throw new IllegalStateException(var10);
}
try {
listeners.running(context); //监听器们运行应用程序上下文
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}