美文网首页
SpringBoot源码解析之属性配置文件

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

作者: handsomemao666 | 来源:发表于2020-11-11 10:39 被阅读0次

    一、 属性配置方式

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

    • 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属性集
    1. configureEnvironment
    • 添加defaultProperties属性集
    • 添加commandLineArgs属性集
    1. listeners.environmentPrepared(environment);
      监听该事件的监听器为ConfigFileApplicationListener,包含四个后处理器
    • 添加spring_application_json属性集
    • 添加vacp属性集
    • 添加random属性集
    • 添加application-profile.(properties|yml)
      后处理器ConfigFileApplicationListener负责application等文件的处理

    四、profile文件加载

    1. initializeProfiles逻辑


      initializeProfiles逻辑

    相关文章

      网友评论

          本文标题:SpringBoot源码解析之属性配置文件

          本文链接:https://www.haomeiwen.com/subject/eehtsktx.html