DefaultListableBeanFactory allows for init methods to register further bean definitions (again; SPR-7757)

This commit is contained in:
Juergen Hoeller 2011-07-27 22:51:26 +00:00
parent fad865fbad
commit f432f3043f
1 changed files with 8 additions and 6 deletions

View File

@ -543,7 +543,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
} }
/** /**
* Considers all beans as eligible for metdata caching * Considers all beans as eligible for metadata caching
* if the factory's configuration has been marked as frozen. * if the factory's configuration has been marked as frozen.
* @see #freezeConfiguration() * @see #freezeConfiguration()
*/ */
@ -556,9 +556,11 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
if (this.logger.isInfoEnabled()) { if (this.logger.isInfoEnabled()) {
this.logger.info("Pre-instantiating singletons in " + this); this.logger.info("Pre-instantiating singletons in " + this);
} }
synchronized (this.beanDefinitionMap) { synchronized (this.beanDefinitionMap) {
for (String beanName : this.beanDefinitionNames) { // Iterate over a copy to allow for init methods which in turn register new bean definitions.
// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
List<String> beanNames = new ArrayList<String>(this.beanDefinitionNames);
for (String beanName : beanNames) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
if (isFactoryBean(beanName)) { if (isFactoryBean(beanName)) {
@ -572,7 +574,8 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
}, getAccessControlContext()); }, getAccessControlContext());
} }
else { else {
isEagerInit = factory instanceof SmartFactoryBean && ((SmartFactoryBean) factory).isEagerInit(); isEagerInit = (factory instanceof SmartFactoryBean &&
((SmartFactoryBean) factory).isEagerInit());
} }
if (isEagerInit) { if (isEagerInit) {
getBean(beanName); getBean(beanName);
@ -666,8 +669,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
destroySingleton(beanName); destroySingleton(beanName);
} }
// Reset all bean definitions that have the given bean as parent // Reset all bean definitions that have the given bean as parent (recursively).
// (recursively).
for (String bdName : this.beanDefinitionNames) { for (String bdName : this.beanDefinitionNames) {
if (!beanName.equals(bdName)) { if (!beanName.equals(bdName)) {
BeanDefinition bd = this.beanDefinitionMap.get(bdName); BeanDefinition bd = this.beanDefinitionMap.get(bdName);