SpringIOC
声明: 此博客仅记录本人DUBUG源码步骤
Rough diagram:
- Load the xml file
setConfigLocations method
- Call the Refresh method
Refresh method
The prepare* methods are all preparations!
@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;
}
}
}
prepareRefresh() method
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // 创建BeanFactory对象
refreshBeanFactory method
prepareBeanFactory(beanFactory);
The standard context features of the configuration factory, such as the class loader and post processor of the context.
postProcessBeanFactory(beanFactory);
Subclass extension
invokeBeanFactoryPostProcessors(beanFactory)
Instantiate and execute BeanFactoryPostProcessors!
registerBeanPostProcessors(beanFactory)
Register BeanPostProcessor bean
initMessageSource();
International processing!
initApplicationEventMulticaster()
Initialize a broadcaster!
registerListeners()
Register the listener!
finishBeanFactoryInitialization(beanFactory);
Instantiate bean
@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);
}
}
}
In the doGetBean method
In createBean method
In the createBeanInstance method, Bean objects are created through a series of reflection logic
Use attribute values to populate Bean instances in a given BeanWrapper
At this point, the Bean is created and instantiated!
finishRefresh()
To complete the refresh of this context, call the onRefresh() method of LifecycleProcessor and publish
Attachment: Bean's life cycle