From df9d2bc9f47381a2f9e6435f825eb71e63840393 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 25 Apr 2017 10:47:26 -0700 Subject: [PATCH] Remove @ConditionalOnProperty 'relaxed' attribute Remove the `relaxed` attribute from `@ConditionalOnProperty` and instead rely on the direct configuration property source relaxed name support. Closes gh-9003 --- .../condition/ConditionalOnProperty.java | 8 +-- .../condition/OnPropertyCondition.java | 9 +-- .../condition/ConditionalOnPropertyTests.java | 62 +++++-------------- 3 files changed, 16 insertions(+), 63 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnProperty.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnProperty.java index de09c90acf2..f9db375c29c 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnProperty.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnProperty.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -122,10 +122,4 @@ public @interface ConditionalOnProperty { */ boolean matchIfMissing() default false; - /** - * If relaxed names should be checked. Defaults to {@code true}. - * @return if relaxed names are used - */ - boolean relaxedNames() default true; - } 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 d5b8ace68e9..c084a7840b4 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 @@ -23,7 +23,6 @@ import java.util.Map; import java.util.Map.Entry; import org.springframework.boot.autoconfigure.condition.ConditionMessage.Style; -import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.Ordered; @@ -121,8 +120,6 @@ class OnPropertyCondition extends SpringBootCondition { private final String[] names; - private final boolean relaxedNames; - private final boolean matchIfMissing; Spec(AnnotationAttributes annotationAttributes) { @@ -133,7 +130,6 @@ class OnPropertyCondition extends SpringBootCondition { this.prefix = prefix; this.havingValue = annotationAttributes.getString("havingValue"); this.names = getNames(annotationAttributes); - this.relaxedNames = annotationAttributes.getBoolean("relaxedNames"); this.matchIfMissing = annotationAttributes.getBoolean("matchIfMissing"); } @@ -149,11 +145,8 @@ class OnPropertyCondition extends SpringBootCondition { private void collectProperties(PropertyResolver resolver, List missing, List nonMatching) { - if (this.relaxedNames) { - resolver = new RelaxedPropertyResolver(resolver, this.prefix); - } for (String name : this.names) { - String key = (this.relaxedNames ? name : this.prefix + name); + String key = this.prefix + name; if (resolver.containsProperty(key)) { if (!isMatch(resolver.getProperty(key), this.havingValue)) { nonMatching.add(name); 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 13ae5bb448e..d0c3d4344d4 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,10 +26,14 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.test.util.EnvironmentTestUtils; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.StandardEnvironment; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.containsString; @@ -48,7 +52,9 @@ public class ConditionalOnPropertyTests { @Rule public ExpectedException thrown = ExpectedException.none(); - private AnnotationConfigApplicationContext context; + private ConfigurableApplicationContext context; + + private ConfigurableEnvironment environment = new StandardEnvironment(); @After public void tearDown() { @@ -98,13 +104,6 @@ public class ConditionalOnPropertyTests { assertThat(this.context.containsBean("foo")).isTrue(); } - @Test - public void nonRelaxedName() throws Exception { - load(NonRelaxedPropertiesRequiredConfiguration.class, - "theRelaxedProperty=value1"); - assertThat(this.context.containsBean("foo")).isFalse(); - } - @Test // Enabled by default public void enabledIfNotConfiguredOtherwise() { @@ -185,18 +184,6 @@ public class ConditionalOnPropertyTests { assertThat(this.context.containsBean("foo")).isTrue(); } - @Test - public void strictNameMatch() { - load(StrictNameConfig.class, "simple.my-property:bar"); - assertThat(this.context.containsBean("foo")).isTrue(); - } - - @Test - public void strictNameNoMatch() { - load(StrictNameConfig.class, "simple.myProperty:bar"); - assertThat(this.context.containsBean("foo")).isFalse(); - } - @Test public void multiValuesAllSet() { load(MultiValuesConfig.class, "simple.my-property:bar", @@ -271,10 +258,9 @@ public class ConditionalOnPropertyTests { } private void load(Class config, String... environment) { - this.context = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(this.context, environment); - this.context.register(config); - this.context.refresh(); + EnvironmentTestUtils.addEnvironment(this.environment, environment); + this.context = new SpringApplicationBuilder(config).environment(this.environment) + .web(WebApplicationType.NONE).run(); } @Configuration @@ -310,17 +296,6 @@ public class ConditionalOnPropertyTests { } - @Configuration - @ConditionalOnProperty(name = "the-relaxed-property", relaxedNames = false) - protected static class NonRelaxedPropertiesRequiredConfiguration { - - @Bean - public String foo() { - return "foo"; - } - - } - @Configuration // i.e ${simple.myProperty:true} @ConditionalOnProperty(prefix = "simple", name = "my-property", havingValue = "true", matchIfMissing = true) @@ -378,17 +353,6 @@ public class ConditionalOnPropertyTests { } - @Configuration - @ConditionalOnProperty(prefix = "simple", name = "my-property", havingValue = "bar", relaxedNames = false) - static class StrictNameConfig { - - @Bean - public String foo() { - return "foo"; - } - - } - @Configuration @ConditionalOnProperty(prefix = "simple", name = { "my-property", "my-another-property" }, havingValue = "bar") @@ -434,6 +398,7 @@ public class ConditionalOnPropertyTests { } + @Configuration @ConditionalOnMyFeature protected static class MetaAnnotation { @@ -444,6 +409,7 @@ public class ConditionalOnPropertyTests { } + @Configuration @ConditionalOnMyFeature @ConditionalOnProperty(prefix = "my.other.feature", name = "enabled", havingValue = "true", matchIfMissing = false) protected static class MetaAnnotationAndDirectAnnotation {