From 3af54692fa628708cc7e5eab36ada85f01a519df Mon Sep 17 00:00:00 2001 From: liuhy365 Date: Mon, 13 Apr 2020 23:25:51 +0800 Subject: [PATCH] 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. --- .../beans/factory/support/AbstractBeanFactory.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java index 6d028481ca..4709420e35 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java @@ -769,6 +769,9 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp if (this.parentBeanFactory != null && this.parentBeanFactory != parentBeanFactory) { throw new IllegalStateException("Already associated with parent BeanFactory: " + this.parentBeanFactory); } + if(this == parentBeanFactory) { + throw new IllegalStateException("Can not set parent bean factory to self."); + } this.parentBeanFactory = parentBeanFactory; }