Skip to content

Commit

Permalink
Allow child context to override parent's configuration properties
Browse files Browse the repository at this point in the history
Closes gh-41487
  • Loading branch information
wilkinsona committed Jul 24, 2024
1 parent 12ec18f commit 09fdb9d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
Expand All @@ -17,7 +17,6 @@
package org.springframework.boot.context.properties;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.HierarchicalBeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
Expand Down Expand Up @@ -68,18 +67,8 @@ private String getName(Class<?> type, MergedAnnotation<ConfigurationProperties>
}

private boolean containsBeanDefinition(String name) {
return containsBeanDefinition(this.beanFactory, name);
}

private boolean containsBeanDefinition(BeanFactory beanFactory, String name) {
if (beanFactory instanceof ListableBeanFactory listableBeanFactory
&& listableBeanFactory.containsBeanDefinition(name)) {
return true;
}
if (beanFactory instanceof HierarchicalBeanFactory hierarchicalBeanFactory) {
return containsBeanDefinition(hierarchicalBeanFactory.getParentBeanFactory(), name);
}
return false;
return (this.beanFactory instanceof ListableBeanFactory listableBeanFactory
&& listableBeanFactory.containsBeanDefinition(name));
}

private void registerBeanDefinition(String beanName, Class<?> type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ void loadWhenBindingWithParentContextShouldBind() {
load(new Class<?>[] { BasicConfiguration.class, BasicPropertiesConsumer.class }, "name=child");
assertThat(this.context.getBean(BasicProperties.class)).isNotNull();
assertThat(parent.getBean(BasicProperties.class)).isNotNull();
assertThat(this.context.getBean(BasicPropertiesConsumer.class).getName()).isEqualTo("parent");
assertThat(this.context.getBean(BasicPropertiesConsumer.class).getName()).isEqualTo("child");
parent.close();
}

Expand Down

0 comments on commit 09fdb9d

Please sign in to comment.