Given some application configuration with an unresolvable placeholder, like the following application.yml
my:
thing: ${missing-placeholder}/whatever
When I use @Value
annotations, the placeholders in the configuration file are validated, so in this case:
package com.test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class PropValues {
@Value("${my.thing}") String thing;
public String getThing() { return thing; }
}
I get an IllegalArgumentException: Could not resolve placeholder 'missing-placeholder' in value "${missing-placeholder}/whatever"
. This is because the value is being set directly by AbstractBeanFactory.resolveEmbeddedValue
and there is nothing to catch the exception thrown by PropertyPlaceholderHelper.parseStringValue
However, looking to move to @ConfigurationProperties
style I noticed that this validation is missing, for example in this case:
package com.test;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
@ConfigurationProperties(prefix = "my")
public class Props {
private String thing;
public String getThing() { return thing; }
public void setThing(String thing) { this.thing = thing; }
}
there is no exception. I can see PropertySourcesPropertyValues.getEnumerableProperty
catches the exception with the comment // Probably could not resolve placeholders, ignore it here
and gathers the invalid value into its internal map. Subsequent data binding does not check for unresolved placeholders.
I checked that simply applying the @Validated
and @Valid
annotations to the class and field do not help.
Is there any way to preserve the behaviour of throwing an exception on unresolved placeholders with ConfigurationProperties
binding?
@Validated
on th class and@NotNull
or@NotEmpty
on the field and for validation to work you will have to have a JSR-303 validator on your class path likehibernate-validation
. Only adding the annotation@Validation
yields nothing. – Ar