1

1.实现EnvironmentPostProcessor接口

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);
    }
 
}

2.在META-INF的spring.factories中添加以下配置

org.springframework.boot.env.EnvironmentPostProcessor=\ com.xxxxx.MyEnvironmentPostProcessor

逐梦小生
222 声望249 粉丝