前言:springboot近些年已经逐渐成为新项目的必备框架。Springboot可以只使用几行代码就可以帮我们搭建一个可运行的项目框架。Springboot的自定义配置也是非常简单,只需要遵循规范在application.yml或application.properties中添加配置,就可以在代码中使用@Value获取到配置。那么springboot就的合适加载的自定义配置?自定义配置又是存储在哪里的呢?
Springboot的启动类很简单,一行代码就搞定。
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
要想搞清楚上面两个问题我们只能从springboot的源码中寻找答案。
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);
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
通过不断的猜测与debug尝试,可以发现,在执行完prepareEnvironment后,environment对象中可以找到application.properties中的配置。
到这里我们知道配置存储的位置了。仔细观察可以发现environment中同时存储了项目的启动变量以及操作系统的环境变量等。
配置文件的具体加载流程请看下面的时序图:
从时序图可以看出加载的流程是很简单的,但其中涉及到监听器、Environment的后置处理器、属性源加载器等组件。以及springboot的SPI机制。
ConfigFileApplicationListener:既是事件监听器同时也是Environment的后置处理器。
PropertySourceLoader:两个实现类PropertiesPropertySourceLoader,YamlPropertySourceLoader它们分别实现properties、yml文件的加载。
理解springboot配置加载流程后,我们想加载远程配置(统一配置中心)的时候可以通过SPI机制自定义Environment后置处理器实现。