SpringIOC
声明: 此博客仅记录本人DUBUG源码步骤
Diagrama aproximado:
- Cargue el archivo xml
método setConfigLocations
- Llamar al método Refresh
Método de actualización
¡Los métodos de preparación * son todos preparativos!
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
// 准备工作
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
// 实例化BeanFactory
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
// 准备工作,设置或忽略Factory中的一些对箱子
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
// 做扩展
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
// 实例化并执行BeanFactoryPostProcessors
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
// 注册BeanPostProcessors
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
// 国际化处理
initMessageSource();
// Initialize event multicaster for this context.
// 初始化一个广播器
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
// 扩展
onRefresh();
// Check for listener beans and register them.
// 注册监听器
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
// 实例化Bean
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
// 刷新
finishRefresh();
}
catch (BeansException ex) {
logger.warn("Exception encountered during context initialization - cancelling refresh attempt", ex);
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
}
}
método prepareRefresh ()
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // 创建BeanFactory对象
método refreshBeanFactory
prepareBeanFactory(beanFactory);
Las características de contexto estándar de la fábrica de configuración, como el cargador de clases y el postprocesador del contexto.
postProcessBeanFactory(beanFactory);
Extensión de subclase
invokeBeanFactoryPostProcessors(beanFactory)
¡Cree una instancia y ejecute BeanFactoryPostProcessors!
registerBeanPostProcessors(beanFactory)
Registrar bean BeanPostProcessor
initMessageSource();
Procesamiento internacional!
initApplicationEventMulticaster()
¡Inicialice una emisora!
registerListeners()
¡Registre al oyente!
finishBeanFactoryInitialization (beanFactory);
Instancia de frijol
@Override
public void preInstantiateSingletons() throws BeansException {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Pre-instantiating singletons in " + this);
}
// 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);
// Trigger initialization of all non-lazy singleton beans...
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>() {
@Override
public Boolean run() {
return ((SmartFactoryBean<?>) factory).isEagerInit();
}
}, getAccessControlContext());
}
else {
isEagerInit = (factory instanceof SmartFactoryBean &&
((SmartFactoryBean<?>) factory).isEagerInit());
}
if (isEagerInit) {
getBean(beanName);
}
}
else {
getBean(beanName);
}
}
}
En el método doGetBean
En el método createBean
En el método createBeanInstance, los objetos Bean se crean mediante una serie de lógica de reflexión
Utilice valores de atributo para poblar instancias de Bean en un BeanWrapper dado
En este punto, el Bean se crea y se crea una instancia.
finishRefresh ()
Para completar la actualización de este contexto, llame al método onRefresh () de LifecycleProcessor y publique
Adjunto: ciclo de vida de Bean