org.springframework.boot.SpringApplication
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments) { ConfigurableEnvironment environment = getOrCreateEnvironment(); configureEnvironment(environment, applicationArguments.getSourceArgs()); ConfigurationPropertySources.attach(environment); listeners.environmentPrepared(environment); bindToSpringApplication(environment); if (!this.isCustomEnvironment) { environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment, deduceEnvironmentClass()); } ConfigurationPropertySources.attach(environment); return environment; }
|
EnvironmentPostProcessor 接口的实现类定义在 META-INF/spring.factories
文件中, Spring Boot 应用启动时, 调用上面的 prepareEnvironment
方法时,会发布 ApplicationEnvironmentPreparedEvent
事件,相应的监听器类 ConfigFileApplicationListener
监听到改时间后,会通过类似于 java SPI
机制加载所有指定 EnvironmentPostProcessor 接口的实现类,并且初始化,调用内部方法 postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application)
,实现自定义 spring 的 Environment
spring.factories 机制
META-INF