DefaultSingletonBeanRegistry's isDependent defensively checks for circular recursion
Issue: SPR-10787
This commit is contained in:
parent
1daa5eb383
commit
15d3b88037
|
@ -18,6 +18,7 @@ package org.springframework.beans.factory.support;
|
|||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
|
@ -441,9 +442,17 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
|
|||
* dependent on the given bean or on any of its transitive dependencies.
|
||||
* @param beanName the name of the bean to check
|
||||
* @param dependentBeanName the name of the dependent bean
|
||||
* @since 4.0
|
||||
*/
|
||||
protected boolean isDependent(String beanName, String dependentBeanName) {
|
||||
return isDependent(beanName, dependentBeanName, null);
|
||||
}
|
||||
|
||||
private boolean isDependent(String beanName, String dependentBeanName, Set<String> alreadySeen) {
|
||||
String canonicalName = canonicalName(beanName);
|
||||
if (alreadySeen != null && alreadySeen.contains(beanName)) {
|
||||
return false;
|
||||
}
|
||||
Set<String> dependentBeans = this.dependentBeanMap.get(canonicalName);
|
||||
if (dependentBeans == null) {
|
||||
return false;
|
||||
|
@ -452,7 +461,11 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
|
|||
return true;
|
||||
}
|
||||
for (String transitiveDependency : dependentBeans) {
|
||||
if (isDependent(transitiveDependency, dependentBeanName)) {
|
||||
if (alreadySeen == null) {
|
||||
alreadySeen = new HashSet<String>();
|
||||
}
|
||||
alreadySeen.add(beanName);
|
||||
if (isDependent(transitiveDependency, dependentBeanName, alreadySeen)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,12 +14,12 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans.factory;
|
||||
package org.springframework.beans.factory.support;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
|
||||
import org.springframework.beans.factory.ObjectFactory;
|
||||
import org.springframework.tests.sample.beans.DerivedTestBean;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
|
||||
|
@ -83,4 +83,22 @@ public class DefaultSingletonBeanRegistryTests {
|
|||
assertTrue(tb.wasDestroyed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDependentRegistration() {
|
||||
DefaultSingletonBeanRegistry beanRegistry = new DefaultSingletonBeanRegistry();
|
||||
|
||||
beanRegistry.registerDependentBean("a", "b");
|
||||
beanRegistry.registerDependentBean("b", "c");
|
||||
beanRegistry.registerDependentBean("c", "b");
|
||||
assertTrue(beanRegistry.isDependent("a", "b"));
|
||||
assertTrue(beanRegistry.isDependent("b", "c"));
|
||||
assertTrue(beanRegistry.isDependent("c", "b"));
|
||||
assertTrue(beanRegistry.isDependent("a", "c"));
|
||||
assertFalse(beanRegistry.isDependent("c", "a"));
|
||||
assertFalse(beanRegistry.isDependent("b", "a"));
|
||||
assertFalse(beanRegistry.isDependent("a", "a"));
|
||||
assertTrue(beanRegistry.isDependent("b", "b"));
|
||||
assertTrue(beanRegistry.isDependent("c", "c"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue