Skip to content

Commit 8c347e6

Browse files
committed
Expose additional methods for Spring Boot
Update DefaultListableBeanFactory and DefaultSingletonBeanRegistry to expose additional methods which can be used by Spring Boot: - Add a isAllowEagerClassLoading getter - Expose the previously protected predictBeanType method - Add getBeanDefinitionNamesCollection and getSingletonNamesCollection alternatives that do not require array copying. This commit also updates several internal methods to use the new getBeanDefinitionNamesCollection() and getSingletonNamesCollection() methods. Issue: SPR-
1 parent 2956049 commit 8c347e6

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
165165
/** List of bean definition names, in registration order */
166166
private final List<String> beanDefinitionNames = new ArrayList<String>();
167167

168+
/** Unmodifiable view of beanDefinitionNames */
169+
private final List<String> unmodifiableBeanDefinitionNames = Collections.unmodifiableList(this.beanDefinitionNames);
170+
168171
/** Whether bean definition metadata may be cached for all beans */
169172
private boolean configurationFrozen = false;
170173

@@ -227,6 +230,15 @@ public void setAllowEagerClassLoading(boolean allowEagerClassLoading) {
227230
this.allowEagerClassLoading = allowEagerClassLoading;
228231
}
229232

233+
/**
234+
* Return if the factory is allowed to eagerly load bean classes.
235+
* @see #setAllowEagerClassLoading(boolean)
236+
* @since 4.1.2
237+
*/
238+
public boolean isAllowEagerClassLoading() {
239+
return this.allowEagerClassLoading;
240+
}
241+
230242
/**
231243
* Set a {@link java.util.Comparator} for dependency Lists and arrays.
232244
* @see org.springframework.core.OrderComparator
@@ -288,6 +300,10 @@ public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory) {
288300
}
289301
}
290302

303+
@Override
304+
public Class<?> predictBeanType(String beanName, RootBeanDefinition mbd, Class<?>... typesToMatch) {
305+
return super.predictBeanType(beanName, mbd, typesToMatch);
306+
}
291307

292308
//---------------------------------------------------------------------
293309
// Implementation of ListableBeanFactory interface
@@ -362,6 +378,14 @@ public String[] getBeanDefinitionNames() {
362378
}
363379
}
364380

381+
/**
382+
* Return an unmodifiable collection containing the bean definition names.
383+
* @since 4.1.2
384+
*/
385+
public Collection<String> getBeanDefinitionNamesCollection() {
386+
return this.unmodifiableBeanDefinitionNames;
387+
}
388+
365389
@Override
366390
public String[] getBeanNamesForType(Class<?> type) {
367391
return getBeanNamesForType(type, true, true);
@@ -389,8 +413,7 @@ private String[] doGetBeanNamesForType(Class<?> type, boolean includeNonSingleto
389413
List<String> result = new ArrayList<String>();
390414

391415
// Check all bean definitions.
392-
String[] beanDefinitionNames = getBeanDefinitionNames();
393-
for (String beanName : beanDefinitionNames) {
416+
for (String beanName : getBeanDefinitionNamesCollection()) {
394417
// Only consider bean as eligible if the bean name
395418
// is not defined as alias for some other bean.
396419
if (!isAlias(beanName)) {
@@ -438,8 +461,7 @@ private String[] doGetBeanNamesForType(Class<?> type, boolean includeNonSingleto
438461
}
439462

440463
// Check singletons too, to catch manually registered singletons.
441-
String[] singletonNames = getSingletonNames();
442-
for (String beanName : singletonNames) {
464+
for (String beanName : getSingletonNamesCollection()) {
443465
// Only check if manually registered.
444466
if (!containsBeanDefinition(beanName)) {
445467
// In case of FactoryBean, match object created by FactoryBean.
@@ -512,13 +534,13 @@ public <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingle
512534
@Override
513535
public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
514536
List<String> results = new ArrayList<String>();
515-
for (String beanName : getBeanDefinitionNames()) {
537+
for (String beanName : getBeanDefinitionNamesCollection()) {
516538
BeanDefinition beanDefinition = getBeanDefinition(beanName);
517539
if (!beanDefinition.isAbstract() && findAnnotationOnBean(beanName, annotationType) != null) {
518540
results.add(beanName);
519541
}
520542
}
521-
for (String beanName : getSingletonNames()) {
543+
for (String beanName : getSingletonNamesCollection()) {
522544
if (!results.contains(beanName) && findAnnotationOnBean(beanName, annotationType) != null) {
523545
results.add(beanName);
524546
}
@@ -529,13 +551,13 @@ public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotation
529551
@Override
530552
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
531553
Map<String, Object> results = new LinkedHashMap<String, Object>();
532-
for (String beanName : getBeanDefinitionNames()) {
554+
for (String beanName : getBeanDefinitionNamesCollection()) {
533555
BeanDefinition beanDefinition = getBeanDefinition(beanName);
534556
if (!beanDefinition.isAbstract() && findAnnotationOnBean(beanName, annotationType) != null) {
535557
results.put(beanName, getBean(beanName));
536558
}
537559
}
538-
for (String beanName : getSingletonNames()) {
560+
for (String beanName : getSingletonNamesCollection()) {
539561
if (!results.containsKey(beanName) && findAnnotationOnBean(beanName, annotationType) != null) {
540562
results.put(beanName, getBean(beanName));
541563
}
@@ -1266,7 +1288,7 @@ private void raiseNoSuchBeanDefinitionException(
12661288
public String toString() {
12671289
StringBuilder sb = new StringBuilder(ObjectUtils.identityToString(this));
12681290
sb.append(": defining beans [");
1269-
sb.append(StringUtils.arrayToCommaDelimitedString(getBeanDefinitionNames()));
1291+
sb.append(StringUtils.collectionToCommaDelimitedString(getBeanDefinitionNamesCollection()));
12701292
sb.append("]; ");
12711293
BeanFactory parent = getParentBeanFactory();
12721294
if (parent == null) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.beans.factory.support;
1818

19+
import java.util.Collection;
1920
import java.util.Collections;
2021
import java.util.HashMap;
2122
import java.util.Iterator;
@@ -93,6 +94,9 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
9394
/** Set of registered singletons, containing the bean names in registration order */
9495
private final Set<String> registeredSingletons = new LinkedHashSet<String>(64);
9596

97+
/** Unmodifiable view of registeredSingletons */
98+
private final Set<String> unmodifiableRegisteredSingletons = Collections.unmodifiableSet(this.registeredSingletons);
99+
96100
/** Names of beans that are currently in creation */
97101
private final Set<String> singletonsCurrentlyInCreation =
98102
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(16));
@@ -299,6 +303,14 @@ public String[] getSingletonNames() {
299303
}
300304
}
301305

306+
/**
307+
* Return an unmodifiable {@link Collections} containing the singleton names.
308+
* @since 4.1.2
309+
*/
310+
public Collection<String> getSingletonNamesCollection() {
311+
return this.unmodifiableRegisteredSingletons;
312+
}
313+
302314
@Override
303315
public int getSingletonCount() {
304316
synchronized (this.singletonObjects) {

0 commit comments

Comments
 (0)