diff --git a/spring-vault-core/src/main/java/org/springframework/vault/annotation/VaultPropertySourceRegistrar.java b/spring-vault-core/src/main/java/org/springframework/vault/annotation/VaultPropertySourceRegistrar.java index b291f32f..16cc7907 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/annotation/VaultPropertySourceRegistrar.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/annotation/VaultPropertySourceRegistrar.java @@ -97,11 +97,13 @@ class VaultPropertySourceRegistrar implements ImportBeanDefinitionRegistrar, Assert.notNull(annotationMetadata, "AnnotationMetadata must not be null!"); Assert.notNull(registry, "BeanDefinitionRegistry must not be null!"); - registry.registerBeanDefinition("VaultPropertySourceRegistrar", - BeanDefinitionBuilder // - .rootBeanDefinition(VaultPropertySourceRegistrar.class) // - .setRole(BeanDefinition.ROLE_INFRASTRUCTURE) // - .getBeanDefinition()); + if (!registry.isBeanNameInUse("VaultPropertySourceRegistrar")) { + registry.registerBeanDefinition("VaultPropertySourceRegistrar", + BeanDefinitionBuilder // + .rootBeanDefinition(VaultPropertySourceRegistrar.class) // + .setRole(BeanDefinition.ROLE_INFRASTRUCTURE) // + .getBeanDefinition()); + } Set propertySources = attributesForRepeatable( annotationMetadata, VaultPropertySources.class.getName(), @@ -135,10 +137,17 @@ class VaultPropertySourceRegistrar implements ImportBeanDefinitionRegistrar, AbstractBeanDefinition beanDefinition = createBeanDefinition(ref, renewal, propertyTransformer, propertyPath); - registry.registerBeanDefinition("vaultPropertySource#" + counter, - beanDefinition); + do { + String beanName = "vaultPropertySource#" + counter; - counter++; + if (!registry.isBeanNameInUse(beanName)) { + registry.registerBeanDefinition(beanName, beanDefinition); + break; + } + + counter++; + } + while (true); } } } diff --git a/spring-vault-core/src/test/java/org/springframework/vault/annotation/VaultPropertySourceIntegrationTests.java b/spring-vault-core/src/test/java/org/springframework/vault/annotation/VaultPropertySourceIntegrationTests.java index e01d603e..eb294360 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/annotation/VaultPropertySourceIntegrationTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/annotation/VaultPropertySourceIntegrationTests.java @@ -24,6 +24,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Import; import org.springframework.core.env.Environment; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; @@ -42,10 +43,18 @@ import static org.assertj.core.api.Assertions.assertThat; @ContextConfiguration public class VaultPropertySourceIntegrationTests { - @VaultPropertySource({ "secret/myapp", "secret/myapp/profile" }) + @Import({ Partial1.class, Partial2.class }) static class Config extends VaultIntegrationTestConfiguration { } + @VaultPropertySource({ "secret/myapp", "secret/myapp/profile" }) + static class Partial1 { + } + + @VaultPropertySource("secret/generic") + static class Partial2 { + } + @Autowired Environment env; @Autowired @@ -63,6 +72,8 @@ public class VaultPropertySourceIntegrationTests { vaultOperations.write("secret/myapp", Collections.singletonMap("myapp", "myvalue")); + vaultOperations.write("secret/generic", + Collections.singletonMap("generic", "generic-value")); vaultOperations.write("secret/myapp/profile", Collections.singletonMap("myprofile", "myprofilevalue")); } @@ -72,6 +83,7 @@ public class VaultPropertySourceIntegrationTests { assertThat(env.getProperty("myapp")).isEqualTo("myvalue"); assertThat(env.getProperty("myprofile")).isEqualTo("myprofilevalue"); + assertThat(env.getProperty("generic")).isEqualTo("generic-value"); } @Test