看下面一段代码,为什么一个@SpringBootApplication注解,就能够自动的执行N多的工作呢?
@EnableAsync
@SpringBootApplication
public class NimitzApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(NimitzApplication.class).web(true).run(args);
}
}
过程
-
1 SpringApplication.run(Object source, String... args)
-
2 核心过程在ConfigurableApplicationContext run(String... args)
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
analyzers = new FailureAnalyzers(context);
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
listeners.finished(context, null);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
- 3 魔法过程
SpringApplication.refreshContext
-> AbstractApplicationContext.invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory)
-> PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors
-> ConfigurationClassPostProcessor.processConfigBeanDefinitions
-> ConfigurationClassParser.parse
-> ConfigurationClassParser.processConfigurationClass
// 最终 sourceClass是所有魔法的入口了
-> ConfigurationClassParser.doProcessConfigurationClass(configClass, sourceClass);
5 @import是最核心的注解,各种功能实际上是由三种类型的实现的
@Configuration classes, ImportSelector and ImportBeanDefinitionRegistrar implementations