前景提示
@ComponentScan 的处理都放在org.springframework.context.annotation.ConfigurationClassParser#doProcessConfigurationClass
1.创建SpringApplication对象
1 private void initialize(Object[] sources) { 2 if (sources != null && sources.length > 0) { 3 this.sources.addAll(Arrays.asList(sources)); 4 }
//判断当前是否是web应用 5 this.webEnvironment = deduceWebEnvironment();
//从类路径下找到META-INF/spring 6 setInitializers((Collection) getSpringFactoriesInstances( 7 ApplicationContextInitializer.class)); 8 setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); 9 this.mainApplicationClass = deduceMainApplicationClass(); 10 }
StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; configureHeadlessProperty();
//获取liestener 从类路径下META-INF/spring.factoise下获取 SpringApplicationRunListeners listeners = getRunListeners(args);
//回调starting方法 listeners.started(); try {
//封装命令行参数 ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); context = createAndRefreshContext(listeners, applicationArguments); afterRefresh(context, applicationArguments); listeners.finished(context, null); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass) .logStarted(getApplicationLog(), stopWatch); } return context; } catch (Throwable ex) { handleRunFailure(context, listeners, ex); throw new IllegalStateException(ex); }