Be lenient with prefix and add period if missing
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user