代码进口
上文【Spring源码阐明】Bean加载流程概览,较量具体地阐明白Spring上下文加载的代码进口,而且在AbstractApplicationContext的refresh要领中,点出了finishBeanFactoryInitialization要领完成了对付所有非懒加载的Bean的初始化。
finishBeanFactoryInitialization要领中挪用了DefaultListableBeanFactory的preInstantiateSingletons要领,本文针对preInstantiateSingletons举办阐明,解读一下Spring是如何初始化Bean实例工具出来的。
DefaultListableBeanFactory的preInstantiateSingletons要领
DefaultListableBeanFactory的preInstantiateSingletons要领,顾名思义,初始化所有的单例Bean,软件开发,看一下要领的界说:
public void preInstantiateSingletons() throws BeansException { if (this.logger.isInfoEnabled()) { this.logger.info("Pre-instantiating singletons in " + this); } synchronized (this.beanDefinitionMap) { // 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); if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { if (isFactoryBean(beanName)) { final FactoryBean factory = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName); boolean isEagerInit; if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) { isEagerInit = AccessController.doPrivileged(new PrivilegedAction<Boolean>() { public Boolean run() { return ((SmartFactoryBean) factory).isEagerInit(); } }, getAccessControlContext()); } else { isEagerInit = (factory instanceof SmartFactoryBean && ((SmartFactoryBean) factory).isEagerInit()); } if (isEagerInit) { getBean(beanName); } } else { getBean(beanName); } } } } }
前面的代码较量简朴,按照beanName拿到BeanDefinition(即Bean的界说)。由于此要领实例化的是所有非懒加载的单例Bean,因此要实例化Bean,劳务派遣管理系统,必需满意11行的三个界说:
(1)不是抽象的
(2)必需是单例的
(3)必需长短懒加载的
接着简朴看一下第12行~第29行的代码,这段代码主要做的是一件工作:首先判定一下Bean是否FactoryBean的实现,接着判定Bean是否SmartFactoryBean的实现,如果Bean是SmartFactoryBean的实现而且eagerInit(这个单词字面意思是盼愿加载,找不到一个好的词语去翻译,意思就是界说了这个Bean需要当即加载的意思)的话,会当即实例化这个Bean。Java开拓人员不需要存眷这段代码,因为SmartFactoryBean根基不会用到,我翻译一下Spring官网对付SmartFactoryBean的界说描写:
代码示例
为了后头的代码阐明利便,事先我界说一个Bean:
package org.xrq.action; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.InitializingBean; public class MultiFunctionBean implements InitializingBean, BeanNameAware, BeanClassLoaderAware { private int propertyA; private int propertyB; public int getPropertyA() { return propertyA; } public void setPropertyA(int propertyA) { this.propertyA = propertyA; } public int getPropertyB() { return propertyB; } public void setPropertyB(int propertyB) { this.propertyB = propertyB; } public void initMethod() { System.out.println("Enter MultiFunctionBean.initMethod()"); } @Override public void setBeanClassLoader(ClassLoader classLoader) { System.out.println("Enter MultiFunctionBean.setBeanClassLoader(ClassLoader classLoader)"); } @Override public void setBeanName(String name) { System.out.println("Enter MultiFunctionBean.setBeanName(String name)"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("Enter MultiFunctionBean.afterPropertiesSet()"); } @Override public String toString() { return "MultiFunctionBean [propertyA=" + propertyA + ", propertyB=" + propertyB + "]"; } }