Spring Boot EnvironmentPostProcessor

说明

允许在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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容