SpringBoot源码解析之属性配置文件
2020-11-11 本文已影响0人
handsomemao666
一、 属性配置方式
SpringBoot有多种属性配置方式,以及优先级,以下是SpringBoot的配置方式与优先级:
-
Devtools global settings properties on your home directory (
~/.spring-boot-devtools.properties
when devtools is active). -
@TestPropertySource
annotations on your tests. -
properties
attribute on your tests. Available on@SpringBootTest
and the test annotations for testing a particular slice of your application. - Command line arguments.
- Properties from
SPRING_APPLICATION_JSON
(inline JSON embedded in an environment variable or system property). -
ServletConfig
init parameters. -
ServletContext
init parameters. - JNDI attributes from
java:comp/env
. - Java System properties (
System.getProperties()
). - OS environment variables.
- A
RandomValuePropertySource
that has properties only inrandom.*
. - Profile-specific application properties outside of your packaged jar (
application-{profile}.properties
and YAML variants). - Profile-specific application properties packaged inside your jar (
application-{profile}.properties
and YAML variants). - Application properties outside of your packaged jar (
application.properties
and YAML variants). - Application properties packaged inside your jar (
application.properties
and YAML variants). -
@PropertySource
annotations on your@Configuration
classes. Please note that such property sources are not added to theEnvironment
until the application context is being refreshed. This is too late to configure certain properties such aslogging.*
andspring.main.*
which are read before refresh begins. - Default properties (specified by setting
SpringApplication.setDefaultProperties
).
以下介绍详细的介绍几种重要的配置方式:
- Application Property Files
SpringApplication
默认加载application.properties
按以下优先级进行加载:- A /config subdirectory of the current directory
- The current directory
- A classpath /config package
- The classpath root
可以通过spring.config.name
和spring.config.location
修改默认的配置文件名和配置文件路径
$ java -jar myproject.jar --spring.config.name=myproject
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
通过自定义后,配置文件的加载顺序见SpringBoot配置文件加载顺序
二、Spring Aware
可以使用Spring容器的功能资源
- BeanNameAware 获得容器中bean名称
- BeanClassLoaderAware 获得类加载器
- BeanFactoryAware 获得bean创建工厂
- EnvitonmentAware 获得环境变量
- EmbeddedValueResolverAware 获取spring容器加载的properties
- ResourceLoaderAware 获得资源加载器
- ApplicationEvenPublisherAware 获得应用事件发布器
- MessageSourceAware 获得文本信息
- ApplicationContextAware 获得当前容器应用上下文
原理:
在ApplicationContextAwareProcessor中的postProcessBeforeInitialization对Aware类进行处理
三、配置加载源码解析
配置加载的入口在SpringApplication中的run()方法内
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
prepareEnvironment中的代码如下
ConfigurableEnvironment environment = getOrCreateEnvironment();
configureEnvironment(environment, applicationArguments.getSourceArgs());
listeners.environmentPrepared(environment);
bindToSpringApplication(environment);
if (!this.isCustomEnvironment) {
environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,deduceEnvironmentClass());
}
ConfigurationPropertySources.attach(environment);
return environment;
下面对prepareEnvironment中的子方法做详细介绍
1.getOrCreateEnvironment()
创建一个ConfigurableEnvironment类,在父类以及子类的构造器中,会初始化特定的配置。
比如StandardServletEnvironment
- 添加ServletConfigInitParams
- 添加ServletConfigInitParams
- 添加Jndi属性集
- 添加SystemProperties属性集
- 添加SystemEnvironment属性集
- configureEnvironment
- 添加defaultProperties属性集
- 添加commandLineArgs属性集
- listeners.environmentPrepared(environment);
监听该事件的监听器为ConfigFileApplicationListener,包含四个后处理器
- 添加spring_application_json属性集
- 添加vacp属性集
- 添加random属性集
- 添加application-profile.(properties|yml)
后处理器ConfigFileApplicationListener负责application等文件的处理
四、profile文件加载
-
initializeProfiles逻辑
initializeProfiles逻辑