|
92 | 92 | * @author Juergen Hoeller
|
93 | 93 | * @author Sam Brannen
|
94 | 94 | * @author Costin Leau
|
| 95 | + * @author Chris Beams |
95 | 96 | * @since 16 April 2001
|
96 | 97 | * @see StaticListableBeanFactory
|
97 | 98 | * @see PropertiesBeanDefinitionReader
|
@@ -135,6 +136,12 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
135 | 136 | /** Map of bean definition objects, keyed by bean name */
|
136 | 137 | private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>();
|
137 | 138 |
|
| 139 | + /** Map of singleton bean names keyed by bean class */ |
| 140 | + private final Map<Class<?>, String[]> singletonBeanNamesByType = new ConcurrentHashMap<Class<?>, String[]>(); |
| 141 | + |
| 142 | + /** Map of non-singleton bean names keyed by bean class */ |
| 143 | + private final Map<Class<?>, String[]> nonSingletonBeanNamesByType = new ConcurrentHashMap<Class<?>, String[]>(); |
| 144 | + |
138 | 145 | /** List of bean definition names, in registration order */
|
139 | 146 | private final List<String> beanDefinitionNames = new ArrayList<String>();
|
140 | 147 |
|
@@ -301,6 +308,21 @@ public String[] getBeanNamesForType(Class<?> type) {
|
301 | 308 | }
|
302 | 309 |
|
303 | 310 | public String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) {
|
| 311 | + if (type == null || !allowEagerInit) { |
| 312 | + return this.doGetBeanNamesForType(type, includeNonSingletons, allowEagerInit); |
| 313 | + } |
| 314 | + Map<Class<?>, String[]> cache = includeNonSingletons ? |
| 315 | + this.nonSingletonBeanNamesByType : this.singletonBeanNamesByType; |
| 316 | + String[] resolvedBeanNames = cache.get(type); |
| 317 | + if (resolvedBeanNames != null) { |
| 318 | + return resolvedBeanNames; |
| 319 | + } |
| 320 | + resolvedBeanNames = this.doGetBeanNamesForType(type, includeNonSingletons, allowEagerInit); |
| 321 | + cache.put(type, resolvedBeanNames); |
| 322 | + return resolvedBeanNames; |
| 323 | + } |
| 324 | + |
| 325 | + private String[] doGetBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) { |
304 | 326 | List<String> result = new ArrayList<String>();
|
305 | 327 |
|
306 | 328 | // Check all bean definitions.
|
@@ -671,6 +693,10 @@ protected void resetBeanDefinition(String beanName) {
|
671 | 693 | destroySingleton(beanName);
|
672 | 694 | }
|
673 | 695 |
|
| 696 | + // Remove any assumptions about by-type mappings |
| 697 | + this.singletonBeanNamesByType.clear(); |
| 698 | + this.nonSingletonBeanNamesByType.clear(); |
| 699 | + |
674 | 700 | // Reset all bean definitions that have the given bean as parent (recursively).
|
675 | 701 | for (String bdName : this.beanDefinitionNames) {
|
676 | 702 | if (!beanName.equals(bdName)) {
|
|
0 commit comments