SpringBoot源码解析之属性配置文件

2020-11-11  本文已影响0人  handsomemao666

一、 属性配置方式

SpringBoot有多种属性配置方式,以及优先级,以下是SpringBoot的配置方式与优先级:

  1. Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
  2. @TestPropertySource annotations on your tests.
  3. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
  4. Command line arguments.
  5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
  6. ServletConfig init parameters.
  7. ServletContext init parameters.
  8. JNDI attributes from java:comp/env.
  9. Java System properties (System.getProperties()).
  10. OS environment variables.
  11. A RandomValuePropertySource that has properties only in random.*.
  12. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
  13. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
  14. Application properties outside of your packaged jar (application.properties and YAML variants).
  15. Application properties packaged inside your jar (application.properties and YAML variants).
  16. @PropertySource annotations on your @Configuration classes. Please note that such property sources are not added to the Environment until the application context is being refreshed. This is too late to configure certain properties such as logging.* and spring.main.* which are read before refresh begins.
  17. Default properties (specified by setting SpringApplication.setDefaultProperties).

以下介绍详细的介绍几种重要的配置方式:

  1. 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.namespring.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容器的功能资源

原理:
在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

  1. configureEnvironment
  1. listeners.environmentPrepared(environment);
    监听该事件的监听器为ConfigFileApplicationListener,包含四个后处理器

四、profile文件加载

  1. initializeProfiles逻辑


    initializeProfiles逻辑
上一篇下一篇

猜你喜欢

热点阅读