美文网首页
SpringBoot引入自定义外部配置文件

SpringBoot引入自定义外部配置文件

作者: 伊夫_艾尔斯 | 来源:发表于2020-03-30 10:46 被阅读0次
    SpringBoot预留有引入外部配置的接口:org.springframework.boot.env.EnvironmentPostProcessor
    Allows for customization of the application's Environment prior to the application context being refreshed.
    

    在应用程序上下文刷新之前允许自定义应用程序的环境。

    1. 源码

    package org.springframework.boot.env;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.core.env.ConfigurableEnvironment;
    import org.springframework.core.env.Environment;
    
    /**
     * Allows for customization of the application's {@link Environment} prior to the
     * application context being refreshed.
     * <p>
     * EnvironmentPostProcessor implementations have to be registered in
     * {@code META-INF/spring.factories}, using the fully qualified name of this class as the
     * key.
     * <p>
     * {@code EnvironmentPostProcessor} processors are encouraged to detect whether Spring's
     * {@link org.springframework.core.Ordered Ordered} interface has been implemented or if
     * the {@link org.springframework.core.annotation.Order @Order} annotation is present and
     * to sort instances accordingly if so prior to invocation.
     *
     * @author Andy Wilkinson
     * @author Stephane Nicoll
     * @since 1.3.0
     */
    @FunctionalInterface
    public interface EnvironmentPostProcessor {
    
        /**
         * Post-process the given {@code environment}.
         * @param environment the environment to post-process
         * @param application the application to which the environment belongs
         */
        void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application);
    
    }
    

    2. 引入方式

      EnvironmentPostProcessor implementations have to be registered in META-INF/spring.factories, 
      using the fully qualified name of this class as the key.
    

    必须使用该类的完全限定名作为key在META-INF/spring.factories中注册EnvironmentPostProcessor的实现;
    所以,可以在SpringBoot项目的resources下面新建META-INF文件夹,在新建文件spring.factories,
    文件内容

    org.springframework.boot.env.EnvironmentPostProcessor=com.hstone.business.app.test.MyConfigLoader
    

    key: 接口全限定名
    val: 自定义实现类的全限定名

    3. 自定义实现类

    • 实现接口: 主要是重写方法
        @Override
        public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
            try {
                String customerConfigUrl = environment.getProperty("customer-config-url");
                Resource resource = new FileSystemResourceLoader().getResource(customerConfigUrl);
                Properties properties = PropertiesLoaderUtils.loadProperties(resource);
                PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource("customerApplicationConfig",properties);
                environment.getPropertySources().addLast(propertiesPropertySource);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    
    > 本示例是先从基础配置中读取到配置文件路径(本机或http的远程地址均可), 然后加载配置到系统的配置中
    
    • 配置优先级:

      SpringBoo可以有多套配置文件,每套配置实现类可以设置读取顺序:

        EnvironmentPostProcessor processors are encouraged to detect whether Spring's
        org.springframework.core.Ordered interface has been implemented or if
        the org.springframework.core.annotation.Order annotation is present and
        to sort instances accordingly if so prior to invocation.
    

    鼓励使用EnvironmentPostProcessor处理器来检测是否使用Spring
    已实现org.springframework.core.Ordered接口

    存在org.springframework.core.annotation.Order注释,
    在调用之前对实例进行相应的排序。

    4. 获取配置文件

    接口方法: org.springframework.core.env.PropertyResolver.getProperty(java.lang.String),
    实现方法: org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(java.lang.String, java.lang.Class<T>, boolean)
    该实现方法是循环从所有配置文件按可以读取属性值,第一次获取到值就解析并返回,所以会有优先顺序(排序优先的配置起效)

    5. SpringBoot系统配置文件加载监听器

    org.springframework.boot.context.config.ConfigFileApplicationListener

    相关文章

      网友评论

          本文标题:SpringBoot引入自定义外部配置文件

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