本文以调试一个实际的SpringBoot启动程序为例,参考流程中主要类类图,来分析其启动逻辑和自动化配置原理。
一、SpringBoot启动流程图
二、SpringBoot启动入口
@EnableScheduling
@EnableAsync
@SpringBootApplication
@EnableAspectJAutoProxy
@ComponentScan(basePackages = {
"cn.gov.zcy.demand",
"cn.gov.zcy.id.util",
"cn.gov.zcy.backlog.sdk",
"com.dtdream.vanyar",
"cn.gov.zcy.base.server.gateway",
"cn.gov.zcy.workflow.sdk"})
@ImportResource(value = "classpath:/spring/*.xml")
@MapperScan(basePackages = {"cn.gov.zcy.demand.dao","cn.gov.zcy.workflow.sdk.dao","cn.gov.zcy.springboot.workflow.core"})
@ImportAutoConfiguration(value = { ExternalAutoConfiguration.class })
public class CenterApplication {
public static void main(String[] args) {
SpringApplication.run(CenterApplication.class, args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
@Bean
public WorkflowSdkConfig workflowSdkConfig(){
WorkflowSdkConfig config = new WorkflowSdkConfig();
config.defaultTaskLogComp(Boolean.FALSE);
config.defaultTimelineComp(Boolean.FALSE);
return config;
}
}
复制代码
SpringBoot 有自己独立的启动类。
三、SpringBoot执行流程
/**
* Run the Spring application, creating and refreshing a new
* {@link ApplicationContext}.
* @param args the application arguments (usually passed from a Java main method)
* @return a running {@link ApplicationContext}
*/
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);