diff --git a/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelector.java b/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelector.java index 35a25b209ff..aaf1471d6b1 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelector.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelector.java @@ -1,5 +1,5 @@ /* - * 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"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ package org.springframework.boot.context.properties; import java.util.ArrayList; import java.util.List; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; @@ -78,7 +79,8 @@ class EnableConfigurationPropertiesImportSelector implements ImportSelector { String prefix = extractPrefix(type); String name = (StringUtils.hasText(prefix) ? prefix + "-" + type.getName() : type.getName()); - if (!registry.containsBeanDefinition(name)) { + if (!containsBeanDefinition( + (ConfigurableListableBeanFactory) registry, name)) { registerBeanDefinition(registry, type, name); } } @@ -119,6 +121,23 @@ class EnableConfigurationPropertiesImportSelector implements ImportSelector { + " annotation found on '" + type.getName() + "'."); } + private boolean containsBeanDefinition( + ConfigurableListableBeanFactory beanFactory, String name) { + + boolean result = beanFactory.containsBeanDefinition(name); + if (result) { + return true; + } + if (beanFactory + .getParentBeanFactory() instanceof ConfigurableListableBeanFactory) { + return containsBeanDefinition( + (ConfigurableListableBeanFactory) beanFactory + .getParentBeanFactory(), + name); + } + return false; + } + } } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java b/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java index c69ac5bc8cb..956512fa881 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java @@ -290,18 +290,24 @@ public class EnableConfigurationPropertiesTests { @Test public void testBindingWithParentContext() { - AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); + AnnotationConfigApplicationContext parent = + new AnnotationConfigApplicationContext(); parent.register(TestConfiguration.class); + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(parent, + "name=parent"); parent.refresh(); - TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, - "name=foo"); this.context.setParent(parent); + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, + "name=child"); this.context.register(TestConfiguration.class, TestConsumer.class); this.context.refresh(); assertThat(this.context.getBeanNamesForType(TestProperties.class).length) + .isEqualTo(0); + assertThat(parent.getBeanNamesForType(TestProperties.class).length) .isEqualTo(1); - assertThat(parent.getBeanNamesForType(TestProperties.class).length).isEqualTo(1); - assertThat(this.context.getBean(TestConsumer.class).getName()).isEqualTo("foo"); + assertThat(this.context.getBean(TestConsumer.class).getName()) + .isEqualTo("parent"); + parent.close(); } @Test