Allow default DevTools properties to be overridden

Previously the default DevTools property source was added in first place
to the environment’s property sources in an unordered environment
post-processor. This made it difficult to override them.

This commit updates the post-processor to be ordered with lowest
precedence and to add the property source in last place. This should
allow any other property sources and to override the defaults.

Closes gh-3851
This commit is contained in:
Andy Wilkinson
2015-10-02 16:38:04 +01:00
parent 19b5e59234
commit 315e63b017
2 changed files with 33 additions and 5 deletions

View File

@@ -22,6 +22,8 @@ import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
@@ -34,6 +36,7 @@ import org.springframework.core.env.PropertySource;
* @author Andy Wilkinson
* @since 1.3.0
*/
@Order(Ordered.LOWEST_PRECEDENCE)
public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostProcessor {
private static final Map<String, Object> PROPERTIES;
@@ -56,7 +59,7 @@ public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostPro
if (isLocalApplication(environment)) {
PropertySource<?> propertySource = new MapPropertySource("refresh",
PROPERTIES);
environment.getPropertySources().addFirst(propertySource);
environment.getPropertySources().addLast(propertySource);
}
}

View File

@@ -90,6 +90,30 @@ public class LocalDevToolsAutoConfigurationTests {
assertThat(resolver.isCacheable(), equalTo(false));
}
@Test
public void defaultPropertyCanBeOverridenFromCommandLine() throws Exception {
this.context = initializeAndRun(Config.class, "--spring.thymeleaf.cache=true");
TemplateResolver resolver = this.context.getBean(TemplateResolver.class);
resolver.initialize();
assertThat(resolver.isCacheable(), equalTo(true));
}
@Test
public void defaultPropertyCanBeOverridenFromUserHomeProperties() throws Exception {
String userHome = System.getProperty("user.home");
System.setProperty("user.home",
new File("src/test/resources/user-home").getAbsolutePath());
try {
this.context = initializeAndRun(Config.class);
TemplateResolver resolver = this.context.getBean(TemplateResolver.class);
resolver.initialize();
assertThat(resolver.isCacheable(), equalTo(true));
}
finally {
System.setProperty("user.home", userHome);
}
}
@Test
public void resourceCachePeriodIsZero() throws Exception {
this.context = initializeAndRun(WebResourcesConfig.class);
@@ -210,17 +234,18 @@ public class LocalDevToolsAutoConfigurationTests {
assertThat(folders, hasKey(new File("src/test/java").getAbsoluteFile()));
}
private ConfigurableApplicationContext initializeAndRun(Class<?> config) {
return initializeAndRun(config, Collections.<String, Object>emptyMap());
private ConfigurableApplicationContext initializeAndRun(Class<?> config,
String... args) {
return initializeAndRun(config, Collections.<String, Object>emptyMap(), args);
}
private ConfigurableApplicationContext initializeAndRun(Class<?> config,
Map<String, Object> properties) {
Map<String, Object> properties, String... args) {
Restarter.initialize(new String[0], false, new MockRestartInitializer(), false);
SpringApplication application = new SpringApplication(config);
application.setDefaultProperties(getDefaultProperties(properties));
application.setWebEnvironment(false);
ConfigurableApplicationContext context = application.run();
ConfigurableApplicationContext context = application.run(args);
return context;
}