From b79132ceffe1e7ac1c587fa382c98338441ac755 Mon Sep 17 00:00:00 2001 From: Maciej Walkowiak Date: Thu, 8 May 2014 16:15:54 +0100 Subject: [PATCH] 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 --- .../condition/OnPropertyCondition.java | 7 +++++-- .../condition/ConditionalOnPropertyTests.java | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnPropertyCondition.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnPropertyCondition.java index 29bc5c1a51..1508a7ae3b 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnPropertyCondition.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnPropertyCondition.java @@ -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 missingProperties = new ArrayList(); + 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"); } - } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyTests.java index 53d77f5aa0..020358846f 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyTests.java @@ -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();