The solution we ended up landing on:
This class will create or concatenate entries to spring.autoconfigure.exclude
public class AutoConfigurationExclusion implements EnvironmentPostProcessor{
private static final String SPRING_EXCLUDE_PROPERTY = "spring.autoconfigure.exclude";
String exclusionList;
public AutoConfigurationExclusion(Class<?> ... classes) {
StringBuilder builder = new StringBuilder();
for (Class<?> clazz : classes) {
if (builder.length() > 0) {
builder.append(",");
}
builder.append(clazz.getCanonicalName());
}
exclusionList = builder.toString();
}
public AutoConfigurationExclusion(String ... classes) {
StringBuilder builder = new StringBuilder();
for (String className : classes) {
if (builder.length() > 0) {
builder.append(",");
}
builder.append(className);
}
exclusionList = builder.toString();
}
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
String excludes = environment.getProperty(SPRING_EXCLUDE_PROPERTY);
if (StringUtils.isBlank(excludes)) {
excludes = exclusionList;
} else {
excludes = excludes + "," + exclusionList;
}
Map<String, Object> map = new HashMap<>();
map.put(SPRING_EXCLUDE_PROPERTY, excludes);
SpringPropertyOverrideHelper.overrideValues(environment.getPropertySources(), map);
}
}
And the static helper to get/create a new property source to hold the "overridden" value(s) and that source is added as the first property source.
public class SpringPropertyOverrideHelper {
private static final String PROPERTY_SOURCE_NAME = "overrideProperties";
public static void overrideValues(MutablePropertySources propertySources, Map<String, Object> propertyMap) {
MapPropertySource target = null;
if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME);
if (source instanceof MapPropertySource) {
target = (MapPropertySource) source;
for (Map.Entry<String, Object> entry : propertyMap.entrySet()) {
target.getSource().put(entry.getKey(), entry.getValue());
}
}
} else {
target = new MapPropertySource(PROPERTY_SOURCE_NAME, propertyMap);
}
if (!propertySources.contains(PROPERTY_SOURCE_NAME)) {
propertySources.addFirst(target);
}
}
private SpringPropertyOverrideHelper() {
}
}