PropertySourcesPlaceholderConfigurer's "ignoreUnresolvablePlaceholders" setting reliably applies to nested placeholders as well

Issue: SPR-10549
This commit is contained in:
Juergen Hoeller
2013-07-31 17:50:44 +02:00
parent 803779d160
commit 127b91fd4f
8 changed files with 131 additions and 56 deletions

View File

@@ -112,8 +112,8 @@ public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerS
* <p>Processing occurs by replacing ${...} placeholders in bean definitions by resolving each
* against this configurer's set of {@link PropertySources}, which includes:
* <ul>
* <li>all {@linkplain org.springframework.core.env.ConfigurableEnvironment#getPropertySources environment property sources}, if an
* {@code Environment} {@linkplain #setEnvironment is present}
* <li>all {@linkplain org.springframework.core.env.ConfigurableEnvironment#getPropertySources
* environment property sources}, if an {@code Environment} {@linkplain #setEnvironment is present}
* <li>{@linkplain #mergeProperties merged local properties}, if {@linkplain #setLocation any}
* {@linkplain #setLocations have} {@linkplain #setProperties been}
* {@linkplain #setPropertiesArray specified}
@@ -139,7 +139,7 @@ public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerS
}
try {
PropertySource<?> localPropertySource =
new PropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, this.mergeProperties());
new PropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, mergeProperties());
if (this.localOverride) {
this.propertySources.addFirst(localPropertySource);
}
@@ -152,7 +152,7 @@ public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerS
}
}
this.processProperties(beanFactory, new PropertySourcesPropertyResolver(this.propertySources));
processProperties(beanFactory, new PropertySourcesPropertyResolver(this.propertySources));
this.appliedPropertySources = this.propertySources;
}

View File

@@ -153,6 +153,25 @@ public class PropertyResourceConfigurerIntegrationTests {
}
}
@Test
public void testPropertyPlaceholderConfigurerWithNestedUnresolvableReference() {
StaticApplicationContext ac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("name", "name${var}");
ac.registerSingleton("tb1", TestBean.class, pvs);
pvs = new MutablePropertyValues();
pvs.add("properties", "var=${m}var\nm=${var2}\nvar2=${m2}");
ac.registerSingleton("configurer1", PropertyPlaceholderConfigurer.class, pvs);
try {
ac.refresh();
fail("Should have thrown BeanDefinitionStoreException");
}
catch (BeanDefinitionStoreException ex) {
// expected
ex.printStackTrace();
}
}
@Ignore // this test was breaking after the 3.0 repackaging
@Test
public void testPropertyPlaceholderConfigurerWithAutowireByType() {

View File

@@ -37,8 +37,6 @@ import static org.junit.Assert.*;
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.*;
/**
* Unit tests for {@link PropertySourcesPlaceholderConfigurer}.
*
* @author Chris Beams
* @since 3.1
*/
@@ -143,7 +141,9 @@ public class PropertySourcesPlaceholderConfigurerTests {
PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
pc.setPropertySources(propertySources);
pc.setProperties(new Properties() {{ put("my.name", "local"); }});
pc.setProperties(new Properties() {{
put("my.name", "local");
}});
pc.setIgnoreUnresolvablePlaceholders(true);
pc.postProcessBeanFactory(bf);
assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}"));
@@ -176,6 +176,38 @@ public class PropertySourcesPlaceholderConfigurerTests {
assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}"));
}
@Test(expected=BeanDefinitionStoreException.class)
public void nestedUnresolvablePlaceholder() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerBeanDefinition("testBean",
genericBeanDefinition(TestBean.class)
.addPropertyValue("name", "${my.name}")
.getBeanDefinition());
PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
pc.setProperties(new Properties() {{
put("my.name", "${bogus}");
}});
pc.postProcessBeanFactory(bf); // should throw
}
@Test
public void ignoredNestedUnresolvablePlaceholder() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerBeanDefinition("testBean",
genericBeanDefinition(TestBean.class)
.addPropertyValue("name", "${my.name}")
.getBeanDefinition());
PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
pc.setProperties(new Properties() {{
put("my.name", "${bogus}");
}});
pc.setIgnoreUnresolvablePlaceholders(true);
pc.postProcessBeanFactory(bf);
assertThat(bf.getBean(TestBean.class).getName(), equalTo("${bogus}"));
}
@Test
public void withNonEnumerablePropertySource() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
@@ -217,7 +249,8 @@ public class PropertySourcesPlaceholderConfigurerTests {
ppc.postProcessBeanFactory(bf);
if (override) {
assertThat(bf.getBean(TestBean.class).getName(), equalTo("local"));
} else {
}
else {
assertThat(bf.getBean(TestBean.class).getName(), equalTo("enclosing"));
}
}