Be lenient with prefix and add period if missing

This commit is contained in:
Dave Syer
2014-05-30 15:08:59 +01:00
parent 3541495041
commit 29cb21c322
2 changed files with 26 additions and 2 deletions

View File

@@ -40,8 +40,11 @@ class OnPropertyCondition extends SpringBootCondition {
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
String prefix = (String) metadata.getAnnotationAttributes(
ConditionalOnProperty.class.getName()).get("prefix");
String prefix = ((String) metadata.getAnnotationAttributes(
ConditionalOnProperty.class.getName()).get("prefix")).trim();
if (!"".equals(prefix) && !prefix.endsWith(".")) {
prefix = prefix + ".";
}
String[] names = (String[]) metadata.getAnnotationAttributes(
ConditionalOnProperty.class.getName()).get("value");
Boolean relaxedNames = (Boolean) metadata.getAnnotationAttributes(

View File

@@ -79,6 +79,16 @@ public class ConditionalOnPropertyTests {
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
public void nonRelaxedName() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context.getEnvironment(),
@@ -110,6 +120,17 @@ public class ConditionalOnPropertyTests {
}
@Configuration
@ConditionalOnProperty(prefix = "spring", value = "property")
protected static class RelaxedPropertiesRequiredConfigurationWithShortPrefix {
@Bean
public String foo() {
return "foo";
}
}
@Configuration
@ConditionalOnProperty(value = "the-relaxed-property", relaxedNames = false)
protected static class NonRelaxedPropertiesRequiredConfiguration {