public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {
private static final String PROPERTY_SOURCE_NAME = "defaultProperties";
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
Map<String, Object> map = new HashMap<>();
map.put("localProperey", "value");
MapPropertySource target = (MapPropertySource) environment.getPropertySources().get(PROPERTY_SOURCE_NAME);
if (target == null) {
target = new MapPropertySource(PROPERTY_SOURCE_NAME, map);
}
environment.getPropertySources().addLast(target);
}
}
在META-INF的spring.factories中添加
org.springframework.boot.env.EnvironmentPostProcessor=\
com.xxxxx.MyEnvironmentPostProcessor
调用时间比使用BootstrapConfiguration(PropertySourceLocator的实现)更早