Update @ConditionalOnProperty to not match false
Update @ConditionalOnProperty so that properties that are present but contain the value `false` are not considered a match. Fixes gh-812
This commit is contained in:
parent
d9bf538e95
commit
b79132ceff
|
@ -21,6 +21,7 @@ import java.util.List;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Condition;
|
import org.springframework.context.annotation.Condition;
|
||||||
import org.springframework.context.annotation.ConditionContext;
|
import org.springframework.context.annotation.ConditionContext;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
@ -42,8 +43,11 @@ class OnPropertyCondition extends SpringBootCondition {
|
||||||
|
|
||||||
List<String> missingProperties = new ArrayList<String>();
|
List<String> missingProperties = new ArrayList<String>();
|
||||||
|
|
||||||
|
Environment environment = context.getEnvironment();
|
||||||
for (String property : onProperties) {
|
for (String property : onProperties) {
|
||||||
if (!context.getEnvironment().containsProperty(property)) {
|
if (!environment.containsProperty(property)
|
||||||
|
|| StringUtils.endsWithIgnoreCase(environment.getProperty(property),
|
||||||
|
"false")) {
|
||||||
missingProperties.add(property);
|
missingProperties.add(property);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,5 +61,4 @@ class OnPropertyCondition extends SpringBootCondition {
|
||||||
+ StringUtils.arrayToCommaDelimitedString(missingProperties
|
+ StringUtils.arrayToCommaDelimitedString(missingProperties
|
||||||
.toArray()) + " not found");
|
.toArray()) + " not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,6 +52,22 @@ public class ConditionalOnPropertyTests {
|
||||||
assertFalse(this.context.containsBean("foo"));
|
assertFalse(this.context.containsBean("foo"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBeanIsNotCreatedWhenPropertyValueEqualsFalse() {
|
||||||
|
EnvironmentTestUtils.addEnvironment(this.context.getEnvironment(),
|
||||||
|
"property1=false", "property2=value2");
|
||||||
|
setupContext();
|
||||||
|
assertFalse(this.context.containsBean("foo"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBeanIsNotCreatedWhenPropertyValueEqualsFALSE() {
|
||||||
|
EnvironmentTestUtils.addEnvironment(this.context.getEnvironment(),
|
||||||
|
"property1=FALSE", "property2=value2");
|
||||||
|
setupContext();
|
||||||
|
assertFalse(this.context.containsBean("foo"));
|
||||||
|
}
|
||||||
|
|
||||||
private void setupContext() {
|
private void setupContext() {
|
||||||
this.context.register(MultiplePropertiesRequiredConfiguration.class);
|
this.context.register(MultiplePropertiesRequiredConfiguration.class);
|
||||||
this.context.refresh();
|
this.context.refresh();
|
||||||
|
|
Loading…
Reference in New Issue