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