- 注解
@SpringBootApplication是一个复合注解
1、@SpringBootConfiguration 包含 @Configuration @component两个注解,标识为配置类,和之前xml里配置bean一个意思
2、@EnableAutoConfigration 开启自动配置。包含@AutoConfigurationPackage (@Import(AutoConfigurationPackages.Registrar.class))
@Import(AutoConfigurationImportSelector.class)两个注解
3、@ComponentScan。基础包或类被自动扫描
- 主要代码如下:
public static void main(String[] args) {
SpringApplication.run(Application.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的静态run()方法,创建SpringApplication实例;
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.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();
}
对SpringApplication的属性进行初始化,如加载meta-info/spring.factories目录下的自动装配文件、设置webApplicationType应用类型、设置监听器、判断是否为主类等
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection exceptionReporters = new ArrayList();
this.configureHeadlessProperty();
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting();
Collection exceptionReporters;
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
this.configureIgnoreBeanInfo(environment);
Banner printedBanner = this.printBanner(environment);
context = this.createApplicationContext();
exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
this.prepareContext(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, exceptionReporters, listeners);
throw new IllegalStateException(var10);
}
try {
listeners.running(context);
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}
设置计时器->开始计时->定义应用配置上下文->headless模式,应该是用来打印banner的->加载SpringApplicationRunListeners监听,加载meta-info/spring.factories->配置环境->打印Banner->创建ApplicationContext上下文->刷新上下文,子类扩展刷新上下文->停止计时->打印启动日志->发送ApplicationStartedEvent事件,标志spring容器已经刷新,此时所有的bean实例都已经加载完毕->查找容器中注册有CommandLineRunner或ApplicationRunner的bean,遍历并执行run方法->发送ApplicationReadyEvent事件,标志SpringApplication已经正在运行,即已经成功启动
ApplicationRunner
@FunctionalInterface
public interface ApplicationRunner {
void run(ApplicationArguments args) throws Exception;
}
CommandLineRunner
@FunctionalInterface
public interface CommandLineRunner {
void run(String... args) throws Exception;
}
实现ApplicationRunner或者CommandLineRunner接口,程序在启动时会在自动执行run方法,根据业务需要在run()方法里写业务逻辑。



