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:
@@ -20,11 +20,12 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.devtools.RemoteUrlPropertyExtractor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
@@ -64,6 +65,8 @@ public class RemoteUrlPropertyExtractorTests {
|
||||
ApplicationContext context = doTest("http://localhost:8080");
|
||||
assertThat(context.getEnvironment().getProperty("remoteUrl"),
|
||||
equalTo("http://localhost:8080"));
|
||||
assertThat(context.getEnvironment().getProperty("spring.thymeleaf.cache"),
|
||||
is(nullValue()));
|
||||
}
|
||||
|
||||
private ApplicationContext doTest(String... args) {
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.devtools.env;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Integration tests for the configuration of development-time properties
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class DevToolPropertiesIntegrationTests {
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void classPropertyConditionIsAffectedByDevToolProperties() {
|
||||
SpringApplication application = new SpringApplication(
|
||||
ClassConditionConfiguration.class);
|
||||
application.setWebEnvironment(false);
|
||||
this.context = application.run();
|
||||
this.context.getBean(ClassConditionConfiguration.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanMethodPropertyConditionIsAffectedByDevToolProperties() {
|
||||
SpringApplication application = new SpringApplication(
|
||||
BeanConditionConfiguration.class);
|
||||
application.setWebEnvironment(false);
|
||||
this.context = application.run();
|
||||
this.context.getBean(MyBean.class);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnProperty("spring.h2.console.enabled")
|
||||
static class ClassConditionConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class BeanConditionConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty("spring.h2.console.enabled")
|
||||
public MyBean myBean() {
|
||||
return new MyBean();
|
||||
}
|
||||
}
|
||||
|
||||
static class MyBean {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.devtools.autoconfigure;
|
||||
package org.springframework.boot.devtools.env;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
@@ -26,21 +26,21 @@ import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.boot.devtools.env.DevToolsHomePropertiesPostProcessor;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link DevToolHomePropertiesPostProcessor}.
|
||||
* Tests for {@link DevToolsHomePropertiesPostProcessor}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class DevToolHomePropertiesPostProcessorTests {
|
||||
public class DevToolsHomePropertiesPostProcessorTests {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temp = new TemporaryFolder();
|
||||
@@ -60,28 +60,26 @@ public class DevToolHomePropertiesPostProcessorTests {
|
||||
".spring-boot-devtools.properties"));
|
||||
properties.store(out, null);
|
||||
out.close();
|
||||
Environment environment = new MockEnvironment();
|
||||
ConfigurableEnvironment environment = new MockEnvironment();
|
||||
MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor();
|
||||
postProcessor.setEnvironment(environment);
|
||||
postProcessor.postProcessBeanFactory(mock(ConfigurableListableBeanFactory.class));
|
||||
postProcessor.postProcessEnvironment(environment, null);
|
||||
assertThat(environment.getProperty("abc"), equalTo("def"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ignoresMissingHomeProperties() throws Exception {
|
||||
Environment environment = new MockEnvironment();
|
||||
ConfigurableEnvironment environment = new MockEnvironment();
|
||||
MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor();
|
||||
postProcessor.setEnvironment(environment);
|
||||
postProcessor.postProcessBeanFactory(mock(ConfigurableListableBeanFactory.class));
|
||||
postProcessor.postProcessEnvironment(environment, null);
|
||||
assertThat(environment.getProperty("abc"), nullValue());
|
||||
}
|
||||
|
||||
private class MockDevToolHomePropertiesPostProcessor extends
|
||||
DevToolHomePropertiesPostProcessor {
|
||||
DevToolsHomePropertiesPostProcessor {
|
||||
|
||||
@Override
|
||||
protected File getHomeFolder() {
|
||||
return DevToolHomePropertiesPostProcessorTests.this.home;
|
||||
return DevToolsHomePropertiesPostProcessorTests.this.home;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user