Commit 29cb21c3 authored by Dave Syer's avatar Dave Syer

Be lenient with prefix and add period if missing

parent 35414950
...@@ -40,8 +40,11 @@ class OnPropertyCondition extends SpringBootCondition { ...@@ -40,8 +40,11 @@ class OnPropertyCondition extends SpringBootCondition {
public ConditionOutcome getMatchOutcome(ConditionContext context, public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) { AnnotatedTypeMetadata metadata) {
String prefix = (String) metadata.getAnnotationAttributes( String prefix = ((String) metadata.getAnnotationAttributes(
ConditionalOnProperty.class.getName()).get("prefix"); ConditionalOnProperty.class.getName()).get("prefix")).trim();
if (!"".equals(prefix) && !prefix.endsWith(".")) {
prefix = prefix + ".";
}
String[] names = (String[]) metadata.getAnnotationAttributes( String[] names = (String[]) metadata.getAnnotationAttributes(
ConditionalOnProperty.class.getName()).get("value"); ConditionalOnProperty.class.getName()).get("value");
Boolean relaxedNames = (Boolean) metadata.getAnnotationAttributes( Boolean relaxedNames = (Boolean) metadata.getAnnotationAttributes(
......
...@@ -79,6 +79,16 @@ public class ConditionalOnPropertyTests { ...@@ -79,6 +79,16 @@ public class ConditionalOnPropertyTests {
assertTrue(this.context.containsBean("foo")); assertTrue(this.context.containsBean("foo"));
} }
@Test
public void prefixWithoutPeriod() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context.getEnvironment(),
"spring.property=value1");
this.context
.register(RelaxedPropertiesRequiredConfigurationWithShortPrefix.class);
this.context.refresh();
assertTrue(this.context.containsBean("foo"));
}
@Test @Test
public void nonRelaxedName() throws Exception { public void nonRelaxedName() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context.getEnvironment(), EnvironmentTestUtils.addEnvironment(this.context.getEnvironment(),
...@@ -110,6 +120,17 @@ public class ConditionalOnPropertyTests { ...@@ -110,6 +120,17 @@ public class ConditionalOnPropertyTests {
} }
@Configuration
@ConditionalOnProperty(prefix = "spring", value = "property")
protected static class RelaxedPropertiesRequiredConfigurationWithShortPrefix {
@Bean
public String foo() {
return "foo";
}
}
@Configuration @Configuration
@ConditionalOnProperty(value = "the-relaxed-property", relaxedNames = false) @ConditionalOnProperty(value = "the-relaxed-property", relaxedNames = false)
protected static class NonRelaxedPropertiesRequiredConfiguration { protected static class NonRelaxedPropertiesRequiredConfiguration {
......
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