Commit df9d2bc9 authored by Phillip Webb's avatar Phillip Webb

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
parent fa45cd57
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -122,10 +122,4 @@ public @interface ConditionalOnProperty { ...@@ -122,10 +122,4 @@ public @interface ConditionalOnProperty {
*/ */
boolean matchIfMissing() default false; boolean matchIfMissing() default false;
/**
* If relaxed names should be checked. Defaults to {@code true}.
* @return if relaxed names are used
*/
boolean relaxedNames() default true;
} }
...@@ -23,7 +23,6 @@ import java.util.Map; ...@@ -23,7 +23,6 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.springframework.boot.autoconfigure.condition.ConditionMessage.Style; 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.Condition;
import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
...@@ -121,8 +120,6 @@ class OnPropertyCondition extends SpringBootCondition { ...@@ -121,8 +120,6 @@ class OnPropertyCondition extends SpringBootCondition {
private final String[] names; private final String[] names;
private final boolean relaxedNames;
private final boolean matchIfMissing; private final boolean matchIfMissing;
Spec(AnnotationAttributes annotationAttributes) { Spec(AnnotationAttributes annotationAttributes) {
...@@ -133,7 +130,6 @@ class OnPropertyCondition extends SpringBootCondition { ...@@ -133,7 +130,6 @@ class OnPropertyCondition extends SpringBootCondition {
this.prefix = prefix; this.prefix = prefix;
this.havingValue = annotationAttributes.getString("havingValue"); this.havingValue = annotationAttributes.getString("havingValue");
this.names = getNames(annotationAttributes); this.names = getNames(annotationAttributes);
this.relaxedNames = annotationAttributes.getBoolean("relaxedNames");
this.matchIfMissing = annotationAttributes.getBoolean("matchIfMissing"); this.matchIfMissing = annotationAttributes.getBoolean("matchIfMissing");
} }
...@@ -149,11 +145,8 @@ class OnPropertyCondition extends SpringBootCondition { ...@@ -149,11 +145,8 @@ class OnPropertyCondition extends SpringBootCondition {
private void collectProperties(PropertyResolver resolver, List<String> missing, private void collectProperties(PropertyResolver resolver, List<String> missing,
List<String> nonMatching) { List<String> nonMatching) {
if (this.relaxedNames) {
resolver = new RelaxedPropertyResolver(resolver, this.prefix);
}
for (String name : this.names) { for (String name : this.names) {
String key = (this.relaxedNames ? name : this.prefix + name); String key = this.prefix + name;
if (resolver.containsProperty(key)) { if (resolver.containsProperty(key)) {
if (!isMatch(resolver.getProperty(key), this.havingValue)) { if (!isMatch(resolver.getProperty(key), this.havingValue)) {
nonMatching.add(name); nonMatching.add(name);
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -26,10 +26,14 @@ import org.junit.Rule; ...@@ -26,10 +26,14 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; 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.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.Bean;
import org.springframework.context.annotation.Configuration; 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.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
...@@ -48,7 +52,9 @@ public class ConditionalOnPropertyTests { ...@@ -48,7 +52,9 @@ public class ConditionalOnPropertyTests {
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
private AnnotationConfigApplicationContext context; private ConfigurableApplicationContext context;
private ConfigurableEnvironment environment = new StandardEnvironment();
@After @After
public void tearDown() { public void tearDown() {
...@@ -98,13 +104,6 @@ public class ConditionalOnPropertyTests { ...@@ -98,13 +104,6 @@ public class ConditionalOnPropertyTests {
assertThat(this.context.containsBean("foo")).isTrue(); assertThat(this.context.containsBean("foo")).isTrue();
} }
@Test
public void nonRelaxedName() throws Exception {
load(NonRelaxedPropertiesRequiredConfiguration.class,
"theRelaxedProperty=value1");
assertThat(this.context.containsBean("foo")).isFalse();
}
@Test @Test
// Enabled by default // Enabled by default
public void enabledIfNotConfiguredOtherwise() { public void enabledIfNotConfiguredOtherwise() {
...@@ -185,18 +184,6 @@ public class ConditionalOnPropertyTests { ...@@ -185,18 +184,6 @@ public class ConditionalOnPropertyTests {
assertThat(this.context.containsBean("foo")).isTrue(); 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 @Test
public void multiValuesAllSet() { public void multiValuesAllSet() {
load(MultiValuesConfig.class, "simple.my-property:bar", load(MultiValuesConfig.class, "simple.my-property:bar",
...@@ -271,10 +258,9 @@ public class ConditionalOnPropertyTests { ...@@ -271,10 +258,9 @@ public class ConditionalOnPropertyTests {
} }
private void load(Class<?> config, String... environment) { private void load(Class<?> config, String... environment) {
this.context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.environment, environment);
EnvironmentTestUtils.addEnvironment(this.context, environment); this.context = new SpringApplicationBuilder(config).environment(this.environment)
this.context.register(config); .web(WebApplicationType.NONE).run();
this.context.refresh();
} }
@Configuration @Configuration
...@@ -310,17 +296,6 @@ public class ConditionalOnPropertyTests { ...@@ -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 @Configuration
// i.e ${simple.myProperty:true} // i.e ${simple.myProperty:true}
@ConditionalOnProperty(prefix = "simple", name = "my-property", havingValue = "true", matchIfMissing = true) @ConditionalOnProperty(prefix = "simple", name = "my-property", havingValue = "true", matchIfMissing = true)
...@@ -378,17 +353,6 @@ public class ConditionalOnPropertyTests { ...@@ -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 @Configuration
@ConditionalOnProperty(prefix = "simple", name = { "my-property", @ConditionalOnProperty(prefix = "simple", name = { "my-property",
"my-another-property" }, havingValue = "bar") "my-another-property" }, havingValue = "bar")
...@@ -434,6 +398,7 @@ public class ConditionalOnPropertyTests { ...@@ -434,6 +398,7 @@ public class ConditionalOnPropertyTests {
} }
@Configuration
@ConditionalOnMyFeature @ConditionalOnMyFeature
protected static class MetaAnnotation { protected static class MetaAnnotation {
...@@ -444,6 +409,7 @@ public class ConditionalOnPropertyTests { ...@@ -444,6 +409,7 @@ public class ConditionalOnPropertyTests {
} }
@Configuration
@ConditionalOnMyFeature @ConditionalOnMyFeature
@ConditionalOnProperty(prefix = "my.other.feature", name = "enabled", havingValue = "true", matchIfMissing = false) @ConditionalOnProperty(prefix = "my.other.feature", name = "enabled", havingValue = "true", matchIfMissing = false)
protected static class MetaAnnotationAndDirectAnnotation { protected static class MetaAnnotationAndDirectAnnotation {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment