Skip to content

Commit 79aa661

Browse files
committed
DefaultListableBeanFactory allows for init methods to register further bean definitions (again; SPR-7757)
1 parent 18540de commit 79aa661

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ public boolean isConfigurationFrozen() {
543543
}
544544

545545
/**
546-
* Considers all beans as eligible for metdata caching
546+
* Considers all beans as eligible for metadata caching
547547
* if the factory's configuration has been marked as frozen.
548548
* @see #freezeConfiguration()
549549
*/
@@ -556,9 +556,11 @@ public void preInstantiateSingletons() throws BeansException {
556556
if (this.logger.isInfoEnabled()) {
557557
this.logger.info("Pre-instantiating singletons in " + this);
558558
}
559-
560559
synchronized (this.beanDefinitionMap) {
561-
for (String beanName : this.beanDefinitionNames) {
560+
// Iterate over a copy to allow for init methods which in turn register new bean definitions.
561+
// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
562+
List<String> beanNames = new ArrayList<String>(this.beanDefinitionNames);
563+
for (String beanName : beanNames) {
562564
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
563565
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
564566
if (isFactoryBean(beanName)) {
@@ -572,7 +574,8 @@ public Boolean run() {
572574
}, getAccessControlContext());
573575
}
574576
else {
575-
isEagerInit = factory instanceof SmartFactoryBean && ((SmartFactoryBean) factory).isEagerInit();
577+
isEagerInit = (factory instanceof SmartFactoryBean &&
578+
((SmartFactoryBean) factory).isEagerInit());
576579
}
577580
if (isEagerInit) {
578581
getBean(beanName);
@@ -666,8 +669,7 @@ protected void resetBeanDefinition(String beanName) {
666669
destroySingleton(beanName);
667670
}
668671

669-
// Reset all bean definitions that have the given bean as parent
670-
// (recursively).
672+
// Reset all bean definitions that have the given bean as parent (recursively).
671673
for (String bdName : this.beanDefinitionNames) {
672674
if (!beanName.equals(bdName)) {
673675
BeanDefinition bd = this.beanDefinitionMap.get(bdName);

0 commit comments

Comments
 (0)