Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0d1eef3

Browse files
committed
Add a property to fail fast for unresolvable placeholders in @ConfigurationProperties
Introduce the `spring.configurationproperties.ignore-unresolvable-placeholders` property to control whether an exception should be thrown when a property placeholder `${...}` cannot be resolved. Signed-off-by: Dmytro Nosan <dimanosan@gmail.com>
1 parent 06f66a8 commit 0d1eef3

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

‎core/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinder.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@
4949
import org.springframework.context.ConfigurableApplicationContext;
5050
import org.springframework.core.annotation.MergedAnnotations;
5151
import org.springframework.core.convert.ConversionService;
52+
import org.springframework.core.env.Environment;
5253
import org.springframework.core.env.PropertySources;
5354
import org.springframework.util.Assert;
55+
import org.springframework.util.PropertyPlaceholderHelper;
56+
import org.springframework.util.SystemPropertyUtils;
5457
import org.springframework.validation.Errors;
5558
import org.springframework.validation.Validator;
5659
import org.springframework.validation.annotation.Validated;
@@ -68,6 +71,8 @@ class ConfigurationPropertiesBinder {
6871

6972
private static final String VALIDATOR_BEAN_NAME = EnableConfigurationProperties.VALIDATOR_BEAN_NAME;
7073

74+
private static final String IGNORE_UNRESOLVABLE_PLACEHOLDER_PROPERTY = "spring.configurationproperties.ignore-unresolvable-placeholders";
75+
7176
private final ApplicationContext applicationContext;
7277

7378
private final PropertySources propertySources;
@@ -191,7 +196,16 @@ private Iterable<ConfigurationPropertySource> getConfigurationPropertySources()
191196
}
192197

193198
private PropertySourcesPlaceholdersResolver getPropertySourcesPlaceholdersResolver() {
194-
return new PropertySourcesPlaceholdersResolver(this.propertySources);
199+
return new PropertySourcesPlaceholdersResolver(this.propertySources, getPropertyPlaceholderHelper());
200+
}
201+
202+
private PropertyPlaceholderHelper getPropertyPlaceholderHelper() {
203+
Environment environment = this.applicationContext.getEnvironment();
204+
boolean ignoreUnresolvablePlaceholders = environment.getProperty(IGNORE_UNRESOLVABLE_PLACEHOLDER_PROPERTY,
205+
Boolean.class, true);
206+
return new PropertyPlaceholderHelper(SystemPropertyUtils.PLACEHOLDER_PREFIX,
207+
SystemPropertyUtils.PLACEHOLDER_SUFFIX, SystemPropertyUtils.VALUE_SEPARATOR,
208+
SystemPropertyUtils.ESCAPE_CHARACTER, ignoreUnresolvablePlaceholders);
195209
}
196210

197211
private List<ConversionService> getConversionServices() {

‎core/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,13 @@
475475
"description": "Config file name.",
476476
"defaultValue": "application"
477477
},
478+
{
479+
"name": "spring.configurationproperties.ignore-unresolvable-placeholders",
480+
"type": "java.lang.Boolean",
481+
"sourceType": "org.springframework.boot.context.properties.ConfigurationPropertiesBinder",
482+
"description": "Whether unresolvable placeholders should be ignored or trigger an exception during the binding of configuration properties",
483+
"defaultValue": "true"
484+
},
478485
{
479486
"name": "spring.jpa.defer-datasource-initialization",
480487
"type": "java.lang.Boolean",

‎core/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,30 @@ void loadWhenHasIgnoreUnknownFieldsFalseAndNoUnknownFieldsShouldBind() {
186186
assertThat(((BasicProperties) bean).name).isEqualTo("foo");
187187
}
188188

189+
@Test
190+
void loadWhenIgnoreUnresolvablePlaceholdersSetsToFalseShouldFail() {
191+
assertThatExceptionOfType(ConfigurationPropertiesBindException.class)
192+
.isThrownBy(() -> load(BasicConfiguration.class, "name=${FOO}",
193+
"spring.configurationproperties.ignore-unresolvable-placeholders=false"))
194+
.withCauseInstanceOf(BindException.class)
195+
.withStackTraceContaining("Could not resolve placeholder 'FOO'");
196+
}
197+
198+
@Test
199+
void loadWhenIgnoreUnresolvablePlaceholdersSetsToTrueShouldNotFail() {
200+
load(BasicConfiguration.class, "name=${FOO}",
201+
"spring.configurationproperties.ignore-unresolvable-placeholders=true");
202+
BasicProperties properties = this.context.getBean(BasicProperties.class);
203+
assertThat(properties.name).isEqualTo("${FOO}");
204+
}
205+
206+
@Test
207+
void loadWhenIgnoreUnresolvablePlaceholdersSetsToTrueByDefaultShouldNotFail() {
208+
load(BasicConfiguration.class, "name=${FOO}");
209+
BasicProperties properties = this.context.getBean(BasicProperties.class);
210+
assertThat(properties.name).isEqualTo("${FOO}");
211+
}
212+
189213
@Test
190214
void loadWhenHasIgnoreUnknownFieldsFalseAndUnknownFieldsShouldFail() {
191215
removeSystemProperties();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /