Support 'required properties' precondition
Users may now call #setRequiredProperties(String...) against the Environment (via its ConfigurablePropertyResolver interface) in order to indicate which properties must be present. Environment#validateRequiredProperties() is invoked by AbstractApplicationContext during the refresh() lifecycle to perform the actual check and a MissingRequiredPropertiesException is thrown if the precondition is not satisfied. Issue: SPR-8323
This commit is contained in:
@@ -21,6 +21,7 @@ import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.lessThan;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition;
|
||||
import static org.springframework.context.ConfigurableApplicationContext.ENVIRONMENT_BEAN_NAME;
|
||||
import static org.springframework.core.env.EnvironmentIntegrationTests.Constants.DERIVED_DEV_BEAN_NAME;
|
||||
@@ -60,6 +61,7 @@ import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.jca.context.ResourceAdapterApplicationContext;
|
||||
import org.springframework.jca.support.SimpleBootstrapContext;
|
||||
import org.springframework.jca.work.SimpleTaskWorkManager;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.mock.env.MockPropertySource;
|
||||
import org.springframework.mock.web.MockServletConfig;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
@@ -580,6 +582,32 @@ public class EnvironmentIntegrationTests {
|
||||
assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void abstractApplicationContextValidatesRequiredPropertiesOnRefresh() {
|
||||
{
|
||||
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.refresh();
|
||||
}
|
||||
|
||||
{
|
||||
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.getEnvironment().setRequiredProperties("foo", "bar");
|
||||
try {
|
||||
ctx.refresh();
|
||||
fail("expected missing property exception");
|
||||
} catch (MissingRequiredPropertiesException ex) {
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.getEnvironment().setRequiredProperties("foo");
|
||||
ctx.setEnvironment(new MockEnvironment().withProperty("foo", "fooValue"));
|
||||
ctx.refresh(); // should succeed
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private DefaultListableBeanFactory newBeanFactoryWithEnvironmentAwareBean() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
|
||||
Reference in New Issue
Block a user