Skip to content

Commit 3af5469

Browse files
liuhy365jhoeller
authored andcommitted
Fix parent bean factory self-reference issue.
If set parent bean factory to self, once try to get an undefined bean, bellow condition if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { ... } will always be true and StackOverflowError will be thrown. Sometimes, this issue is hard to detect during runtime, if self-reference is not allowed here, error will be found at the early time of startup. Also, a self-reference parent bean factory is valueless.
1 parent 21887ad commit 3af5469

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,9 @@ public void setParentBeanFactory(@Nullable BeanFactory parentBeanFactory) {
769769
if (this.parentBeanFactory != null && this.parentBeanFactory != parentBeanFactory) {
770770
throw new IllegalStateException("Already associated with parent BeanFactory: " + this.parentBeanFactory);
771771
}
772+
if(this == parentBeanFactory) {
773+
throw new IllegalStateException("Can not set parent bean factory to self.");
774+
}
772775
this.parentBeanFactory = parentBeanFactory;
773776
}
774777

0 commit comments

Comments
 (0)