SpringApplication调用的最终的构造方法
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
//必须要指定PrimarySources
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
//推断当前应用的类型是 servlet 或 reactive 或 NoWeb
this.webApplicationType = WebApplicationType.deduceFromClasspath();
//从META-INF/spring.factories中获取ApplicationContextInitializer
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class));
//从META-INF/spring.factories中获取ApplicationListener ①
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
//推断执行的main方法的定义类
this.mainApplicationClass = deduceMainApplicationClass();
}
run方法的调用过程
public ConfigurableApplicationContext run(String... args) {
//开启计时器
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
//这是设置系统属性java.awt.headless 需要百度了解,也可以忽略
configureHeadlessProperty();
//从META-INF/spring.factories中获取SpringApplicationRunListeners ①
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
//包装命令行参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
//准备好了环境,此刻Environment中都有哪些配置参数了?
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
//打印启动程序的图标(默认是spring的那个)
Banner printedBanner = printBanner(environment);
//创建ApplicationContext
context = createApplicationContext();
//从META-INF/spring.factories中获取SpringBootExceptionReporter
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
//准备ApplicationContext
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
//刷新ApplicationContext
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
//发布started事件
listeners.started(context);
//执行所有的runners
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
//发布running事件
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
①ApplicationListener 和 SpringApplicationRunListeners 的区别
ApplicationListener 是对Spring容器创建及bean 的监听
SpringApplicationRunListeners 是对SpringApplication运行过程的监听 其实就是监听run方法运行的
主要是监听的时机不同
private ConfigurableEnvironment prepareEnvironment(
SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {
// Create and configure the environment
// 根据应用类型 创建对用的Environment对象 会装载环境变量 系统参数 具体应用类型配置参数
ConfigurableEnvironment environment = getOrCreateEnvironment();
// 配置环境: 加入命令行参数PropertySource 配置启用的profiles
configureEnvironment(environment, applicationArguments.getSourceArgs());
// 触发RunListener环境准备完成回调
listeners.environmentPrepared(environment);
//将environment绑定到SpringApplication
bindToSpringApplication(environment);
if (!this.isCustomEnvironment) {
environment = new EnvironmentConverter(getClassLoader())
.convertEnvironmentIfNecessary(environment, deduceEnvironmentClass());
}
ConfigurationPropertySources.attach(environment);
return environment;
}
到这里可能会发现 并没有看到哪里加载配置文件。具体加载配置文件的类是 ConfigFileApplicationListener.postProcessEnvironment方法
此方法被自己类中的onApplicationEvent方法调用的。
onApplicationEvent方法是被EventPublishingRunListener.environmentPrepared方法调用
(实际上就是SpringApplicationRunListener的environmentPrepared方法)
这里是创建ConfigurableApplicationContext 就是通过反射创建的
protected ConfigurableApplicationContext createApplicationContext() {
Class<?> contextClass = this.applicationContextClass;
if (contextClass == null) {
try {
switch (this.webApplicationType) {
case SERVLET:
contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
break;
case REACTIVE:
contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
break;
default:
contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
}
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Unable create a default ApplicationContext, "
+ "please specify an ApplicationContextClass",
ex);
}
}
return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}
private void prepareContext(ConfigurableApplicationContext context,
ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments, Banner printedBanner) {
//设置环境对象
context.setEnvironment(environment);
//设置ApplicationContext的beanNameGenerator、resourceLoader、addConversionService
//(如果通过SpringApplication编程方式指定的话)
postProcessApplicationContext(context);
//应用初始化器 ApplicationContext进行初始化处理
applyInitializers(context);
// 发布ApplicationContext准备妥当时间
listeners.contextPrepared(context);
// 打印statup日志信息
if (this.logStartupInfo) {
logStartupInfo(context.getParent() == null);
logStartupProfileInfo(context);
}
//添加 spring boot中特定的单例bean 到 beanFacroty中
// Add boot specific singleton beans
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
if (printedBanner != null) {
beanFactory.registerSingleton("springBootBanner", printedBanner);
}
if (beanFactory instanceof DefaultListableBeanFactory) {
((DefaultListableBeanFactory) beanFactory)
.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
}
// 加载primarySource、其他编程式指定的source 配置类中的Bean定义
// Load the sources
Set<Object> sources = getAllSources();
Assert.notEmpty(sources, "Sources must not be empty");
load(context, sources.toArray(new Object[0]));
//发布ApplicationContext加载Bean定义完毕事件
listeners.contextLoaded(context);
}
这里就是调用了一下容器的refresh方法
private void refreshContext(ConfigurableApplicationContext context) {
refresh(context);
if (this.registerShutdownHook) {
try {
context.registerShutdownHook();
}
catch (AccessControlException ex) {
// Not allowed in some environments.
}
}
}
容器刷新完毕了,那EnableAutoConfiguration的类是在哪里加载的呢?
EnableAutoConfiguration的加载一定是在refresh方法之前或过程中被调用的,因为它也是bean需要在beanFactory初始化前加载
EnableAutoConfiguration注解上导入了一个类AutoConfigurationImportSelector
AutoConfigurationImportSelector此类是负责处理加了Configuration注解的类