美文网首页
Spring Boot EnvironmentPostProce

Spring Boot EnvironmentPostProce

作者: 核子飞弹 | 来源:发表于2019-06-10 11:28 被阅读0次

    说明

    允许在spring application context refreshed之前定制application运行环境,插入自定义的配置信息

    实现

    在接口实现中,插入自定义PropertySource,可以覆盖已有配置项的值,也可以是全新的配置项

    @Order
    public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {
    
        @Override
        public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
            //MapPropertySource
            Properties properties = new Properties();
            //properties.put("config.item.key", "value");
            PropertiesPropertySource source = new PropertiesPropertySource("custom", properties);
            environment.getPropertySources().addLast(source);
        }
    }
    

    注册

    在resources/META-INF/spring.factories文件中使用全名注册

    org.springframework.boot.env.EnvironmentPostProcessor=\
      com.xxx.config.MyEnvironmentPostProcessor
    

    参考资料

    Spring Boot Config Relaxed Binding

    相关文章

      网友评论

          本文标题:Spring Boot EnvironmentPostProce

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