Update devtools to customise environment before app context is refreshed

Prior to this commit, the devtools used bean factory post processors to
configure the environment with custom, development-time properties. This
meant that the environment was configured as part of the application
context being refreshed. Crucially, this happened after any property
conditions were evaluated making it impossible for the devtools to
change the default auto-configuration behaviour for a bean or
configuration class that was conditional on a property.

This commit moves the configuration of the environment into an
ApplicationListener that listens for the
ApplicationEnvironmentPreparedEvent which is published as soon as the
Environment has been prepared and before the application context is
refreshed.

Closes gh-3726
This commit is contained in:
Andy Wilkinson
2015-08-12 12:14:12 +01:00
parent e370b592d6
commit 9e88dced88
9 changed files with 143 additions and 91 deletions

View File

@@ -23,6 +23,7 @@ import java.util.Map;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.CommandLinePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
@@ -35,9 +36,10 @@ import org.springframework.util.StringUtils;
* {@link RemoteSpringApplication} to use.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
class RemoteUrlPropertyExtractor implements
ApplicationListener<ApplicationEnvironmentPreparedEvent> {
ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
private static final String NON_OPTION_ARGS = CommandLinePropertySource.DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME;
@@ -58,4 +60,9 @@ class RemoteUrlPropertyExtractor implements
environment.getPropertySources().addLast(propertySource);
}
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
}

View File

@@ -53,19 +53,6 @@ import org.springframework.util.StringUtils;
@EnableConfigurationProperties(DevToolsProperties.class)
public class LocalDevToolsAutoConfiguration {
@Autowired
private DevToolsProperties properties;
@Bean
public static DevToolsPropertyDefaultsPostProcessor devToolsPropertyDefaultsPostProcessor() {
return new DevToolsPropertyDefaultsPostProcessor();
}
@Bean
public static DevToolHomePropertiesPostProcessor devToolHomePropertiesPostProcessor() {
return new DevToolHomePropertiesPostProcessor();
}
/**
* Local LiveReload configuration.
*/

View File

@@ -14,63 +14,48 @@
* limitations under the License.
*/
package org.springframework.boot.devtools.autoconfigure;
package org.springframework.boot.devtools.env;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.StringUtils;
/**
* {@link BeanFactoryPostProcessor} to add devtools properties from the users home folder.
* {@link EnvironmentPostProcessor} to add devtools properties from the user's home
* folder.
*
* @author Phillip Webb
* @author Andy Wilkinson
* @since 1.3.0
*/
public class DevToolHomePropertiesPostProcessor implements BeanFactoryPostProcessor,
EnvironmentAware {
public class DevToolsHomePropertiesPostProcessor implements EnvironmentPostProcessor {
private static final String FILE_NAME = ".spring-boot-devtools.properties";
private Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
if (this.environment instanceof ConfigurableEnvironment) {
try {
postProcessEnvironment((ConfigurableEnvironment) this.environment);
}
catch (IOException ex) {
throw new IllegalStateException("Unable to load " + FILE_NAME, ex);
}
}
}
private void postProcessEnvironment(ConfigurableEnvironment environment)
throws IOException {
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
File home = getHomeFolder();
File propertyFile = (home == null ? null : new File(home, FILE_NAME));
if (propertyFile != null && propertyFile.exists() && propertyFile.isFile()) {
FileSystemResource resource = new FileSystemResource(propertyFile);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
environment.getPropertySources().addFirst(
new PropertiesPropertySource("devtools-local", properties));
Properties properties;
try {
properties = PropertiesLoaderUtils.loadProperties(resource);
environment.getPropertySources().addFirst(
new PropertiesPropertySource("devtools-local", properties));
}
catch (IOException ex) {
throw new IllegalStateException("Unable to load " + FILE_NAME, ex);
}
}
}

View File

@@ -14,29 +14,27 @@
* limitations under the License.
*/
package org.springframework.boot.devtools.autoconfigure;
package org.springframework.boot.devtools.env;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
/**
* {@link BeanFactoryPostProcessor} to add properties that make sense when working at
* {@link EnvironmentPostProcessor} to add properties that make sense when working at
* development time.
*
* @author Phillip Webb
* @author Andy Wilkinson
* @since 1.3.0
*/
class DevToolsPropertyDefaultsPostProcessor implements BeanFactoryPostProcessor,
EnvironmentAware {
public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostProcessor {
private static final Map<String, Object> PROPERTIES;
static {
@@ -51,24 +49,18 @@ class DevToolsPropertyDefaultsPostProcessor implements BeanFactoryPostProcessor,
PROPERTIES = Collections.unmodifiableMap(properties);
}
private Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
if (this.environment instanceof ConfigurableEnvironment) {
postProcessEnvironment((ConfigurableEnvironment) this.environment);
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
if (isLocalApplication(environment)) {
PropertySource<?> propertySource = new MapPropertySource("refresh",
PROPERTIES);
environment.getPropertySources().addFirst(propertySource);
}
}
private void postProcessEnvironment(ConfigurableEnvironment environment) {
PropertySource<?> propertySource = new MapPropertySource("refresh", PROPERTIES);
environment.getPropertySources().addFirst(propertySource);
private boolean isLocalApplication(ConfigurableEnvironment environment) {
return environment.getPropertySources().get("remoteUrl") == null;
}
}

View File

@@ -33,7 +33,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.devtools.autoconfigure.DevToolHomePropertiesPostProcessor;
import org.springframework.boot.devtools.autoconfigure.DevToolsProperties;
import org.springframework.boot.devtools.autoconfigure.DevToolsProperties.Restart;
import org.springframework.boot.devtools.autoconfigure.OptionalLiveReloadServer;
@@ -88,11 +87,6 @@ public class RemoteClientConfiguration {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public static DevToolHomePropertiesPostProcessor devToolHomePropertiesPostProcessor() {
return new DevToolHomePropertiesPostProcessor();
}
@Bean
public ClientHttpRequestFactory clientHttpRequestFactory() {
List<ClientHttpRequestInterceptor> interceptors = Arrays

View File

@@ -10,3 +10,8 @@ org.springframework.boot.devtools.restart.RestartApplicationListener
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.devtools.autoconfigure.LocalDevToolsAutoConfiguration,\
org.springframework.boot.devtools.autoconfigure.RemoteDevToolsAutoConfiguration
# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.boot.devtools.env.DevToolsHomePropertiesPostProcessor,\
org.springframework.boot.devtools.env.DevToolsPropertyDefaultsPostProcessor