调用链:
finishBeanFactoryInitialization(beanFactory)
---->DefaultListableBeanFactory.preInstantiateSingletons();
类名:DefaultListableBeanFactory
对符合条件的bean进行初始化,条件就是:
非抽象的,单实例的,非懒加载的
public void preInstantiateSingletons() throws BeansException {
if (this.logger.isInfoEnabled()) {
this.logger.info("Pre-instantiating singletons in " + this);
}
ArrayList beanNames;
synchronized(this.beanDefinitionMap) {
//获取容器中所有的beanName
beanNames = new ArrayList(this.beanDefinitionNames);
}
Iterator i$ = beanNames.iterator();
//对符合条件的bean进行初始化:非抽象的,单实例的,非懒加载的
while(true) {
while(true) {
String beanName;
RootBeanDefinition bd;
do {
do {
do {
if (!i$.hasNext()) {
return;
}
beanName = (String)i$.next();
bd = this.getMergedLocalBeanDefinition(beanName);
} while(bd.isAbstract());
} while(!bd.isSingleton());
} while(bd.isLazyInit());
//如果是factoryBean则按照如下处理,factory获取bean,是getObject方法的返回值
if (this.isFactoryBean(beanName)) {
final FactoryBean<?> factory = (FactoryBean)this.getBean("&" + beanName);
boolean isEagerInit;
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = (Boolean)AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
public Boolean run() {
return ((SmartFactoryBean)factory).isEagerInit();
}
}, this.getAccessControlContext());
} else {
isEagerInit = factory instanceof SmartFactoryBean && ((SmartFactoryBean)factory).isEagerInit();
}
if (isEagerInit) {
this.getBean(beanName);
}
} else {
//通过getBeanName来获取Bean
this.getBean(beanName);
}
}
}
}
通过getBeanName来获取Bean
this.getBean(beanName);
调用链:
AbstractBeanFactory.getBean(java.lang.String)
AbstractBeanFactory.doGetBean