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:
Maciej Walkowiak 2014-05-08 16:15:54 +01:00 committed by Phillip Webb
parent d9bf538e95
commit b79132ceff
2 changed files with 21 additions and 2 deletions

View File

@ -21,6 +21,7 @@ import java.util.List;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.StringUtils;
@ -42,8 +43,11 @@ class OnPropertyCondition extends SpringBootCondition {
List<String> missingProperties = new ArrayList<String>();
Environment environment = context.getEnvironment();
for (String property : onProperties) {
if (!context.getEnvironment().containsProperty(property)) {
if (!environment.containsProperty(property)
|| StringUtils.endsWithIgnoreCase(environment.getProperty(property),
"false")) {
missingProperties.add(property);
}
}
@ -57,5 +61,4 @@ class OnPropertyCondition extends SpringBootCondition {
+ StringUtils.arrayToCommaDelimitedString(missingProperties
.toArray()) + " not found");
}
}

View File

@ -52,6 +52,22 @@ public class ConditionalOnPropertyTests {
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() {
this.context.register(MultiplePropertiesRequiredConfiguration.class);
this.context.refresh();