DefaultListableBeanFactory efficiently accesses current bean names and exposes them via getBeanNamesIterator()

Issue: SPR-12404
This commit is contained in:
Juergen Hoeller
2014-11-01 10:11:20 +01:00
parent 97ea43681b
commit 965bea7b3e
4 changed files with 153 additions and 82 deletions

View File

@@ -1000,6 +1000,16 @@ public class DefaultListableBeanFactoryTests {
beansOfType = lbf.getBeansOfType(null, false, true);
assertEquals(2, beansOfType.size());
Iterator<String> beanNames = lbf.getBeanNamesIterator();
assertEquals("test", beanNames.next());
assertEquals("singletonObject", beanNames.next());
assertFalse(beanNames.hasNext());
assertTrue(lbf.containsSingleton("test"));
assertTrue(lbf.containsSingleton("singletonObject"));
assertTrue(lbf.containsBeanDefinition("test"));
assertFalse(lbf.containsBeanDefinition("singletonObject"));
}
@Test
@@ -1010,8 +1020,8 @@ public class DefaultListableBeanFactoryTests {
p.setProperty("test.name", "Tony");
p.setProperty("test.age", "48");
p.setProperty("test.spouse(ref)", "singletonObject");
p.setProperty("singletonObject.(class)", org.springframework.beans.factory.config.PropertiesFactoryBean.class.getName());
(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
lbf.registerBeanDefinition("singletonObject", new RootBeanDefinition(PropertiesFactoryBean.class));
Object singletonObject = new TestBean();
lbf.registerSingleton("singletonObject", singletonObject);
lbf.preInstantiateSingletons();
@@ -1028,7 +1038,17 @@ public class DefaultListableBeanFactoryTests {
assertTrue(beansOfType.containsValue(singletonObject));
beansOfType = lbf.getBeansOfType(null, false, true);
Iterator<String> beanNames = lbf.getBeanNamesIterator();
assertEquals("test", beanNames.next());
assertEquals("singletonObject", beanNames.next());
assertFalse(beanNames.hasNext());
assertEquals(2, beansOfType.size());
assertTrue(lbf.containsSingleton("test"));
assertTrue(lbf.containsSingleton("singletonObject"));
assertTrue(lbf.containsBeanDefinition("test"));
assertTrue(lbf.containsBeanDefinition("singletonObject"));
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@@ -16,8 +16,6 @@
package org.springframework.beans.factory;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.beans.BeansException;
@@ -32,7 +30,7 @@ import static org.junit.Assert.*;
* @author Chris Beams
* @since 04.07.2006
*/
public final class SharedBeanRegistryTests {
public class DefaultSingletonBeanRegistryTests {
@Test
public void testSingletons() {
@@ -53,9 +51,10 @@ public final class SharedBeanRegistryTests {
assertSame(tb, beanRegistry.getSingleton("tb"));
assertSame(tb2, beanRegistry.getSingleton("tb2"));
assertEquals(2, beanRegistry.getSingletonCount());
assertEquals(2, beanRegistry.getSingletonNames().length);
assertTrue(Arrays.asList(beanRegistry.getSingletonNames()).contains("tb"));
assertTrue(Arrays.asList(beanRegistry.getSingletonNames()).contains("tb2"));
String[] names = beanRegistry.getSingletonNames();
assertEquals(2, names.length);
assertEquals("tb", names[0]);
assertEquals("tb2", names[1]);
beanRegistry.destroySingletons();
assertEquals(0, beanRegistry.getSingletonCount());
@@ -73,8 +72,9 @@ public final class SharedBeanRegistryTests {
assertSame(tb, beanRegistry.getSingleton("tb"));
assertEquals(1, beanRegistry.getSingletonCount());
assertEquals(1, beanRegistry.getSingletonNames().length);
assertTrue(Arrays.asList(beanRegistry.getSingletonNames()).contains("tb"));
String[] names = beanRegistry.getSingletonNames();
assertEquals(1, names.length);
assertEquals("tb", names[0]);
assertFalse(tb.wasDestroyed());
beanRegistry.destroySingletons();