接下去说,上一次最后一个函数里面有三个函数调用,即
destroyBeans(); createBeanFactory() ; loadBeanDefinitions(beanFactory);
1.2.2.1.1 destroyBeans() : 清除当前上下文的所有 bean ,默认的实现为:清除当前上下文的所有缓存的单例 bean ,默认的实现在父类: AbstractApplicationContext ,代码为:
getBeanFactory (). destroySingletons ();
其中: getBeanFactory () 在子类 AbstractRefreshableApplicationContext 中实现,返回该类的一个
DefaultListableBeanFactory 类型的 字段 beanFactory ;
其中: destroySingletons () 定义在接口 ConfigurableBeanFactory 中,由类图发现 DefaultListableBeanFactory 实现了 ConfigurableBeanFactory 接口,该方法在其父类: DefaultSingletonBeanRegistry 里有实现。定义如下:
====================================================================
public void destroySingletons() {
if ( logger .isInfoEnabled()) {
logger .info( “Destroying singletons in “ + this );
}
/** singletonObjects 为一个 MAP 对象,缓存当前的单体实例。 */
synchronized ( this . singletonObjects ) {
/**
* singletonsCurrentlyInDestruction : 我们目前是否在 destroySingletons 。
*/
this . singletonsCurrentlyInDestruction = true ;
}
/**
* disposableBeans 为一个 MAP 对象,缓存当前的一次性 bean 。
*/
synchronized ( this . disposableBeans ) {
String[] disposableBeanNames = StringUtils.toStringArray ( this . disposableBeans .keySet());
for ( int i = disposableBeanNames. length – 1; i >= 0; i–) {
destroySingleton(disposableBeanNames[i]);
}
}
this . containedBeanMap .clear();
this . dependentBeanMap .clear();
this . dependenciesForBeanMap .clear();
synchronized ( this . singletonObjects ) {
this . singletonObjects .clear();
this . singletonFactories .clear();
this . earlySingletonObjects .clear();
this . registeredSingletons .clear();
this . singletonsCurrentlyInDestruction = false ;
}
}
====================================================================
1.2.2.1.1.1 destroySingleton(disposableBeanNames[i]) : 如果给定的 bean 存在则,销毁给定的 bean ,定义如下:
====================================================================
public void destroySingleton(String beanName) {
// 根据给定的名字删除给定 bean
// 销毁相应的一次性 bean 实例
DisposableBean disposableBean = null ;
synchronized ( this . disposableBeans ) {
disposableBean = (DisposableBean) this . disposableBeans .remove(beanName);
destroyBean(beanName, disposableBean);
}
====================================================================
1.2.2.1.1.1.1 protected void removeSingleton(String beanName) :从当前的 factory 中删除给定名字的 bean 。定义如下:
singletonObjects— 缓存单例 bean 的 map
singletonFactories— 缓存单例工厂 bean 的 map
earlySingletonObjects— 缓存早期的单例 bean 的 map
registeredSingletons— 注册的单例 bean 名字的集合,按照注册的顺序存放的 Set
====================================================================
protected void removeSingleton(String beanName) {
synchronized ( this . singletonObjects ) {
this . singletonObjects .remove(beanName);
this . singletonFactories .remove(beanName);
this . earlySingletonObjects .remove(beanName);
this . registeredSingletons .remove(beanName);
}
}
====================================================================
1.2.2.1.1.1.2 destroyBean (beanName, disposableBean); 先销毁依赖于给定 bean 的 bean ,再销毁给定的 bean 。代码如下:
====================================================================
protected void destroyBean(String beanName, DisposableBean bean) {
// 首先销毁依赖 bean
// dependentBeanMap—bean 的 name 和依赖于该 bean 的 bean 的 name 的集合的 map
Set dependencies = (Set ) this . dependentBeanMap .remove(beanName);
if (dependencies != null ) {
if ( logger .isDebugEnabled()) {
logger .debug( “Retrieved dependent beans for bean ‘” + beanName + “‘: “ + dependencies);
}
for (Iterator it = dependencies.iterator(); it.hasNext();) {
String dependentBeanName = (String) it.next();
// 递归调用
destroySingleton(dependentBeanName);
}
}
// 真正开始销毁 bean
if (bean != null ) {
try {
bean.destroy();
}
catch (Throwable ex) {
logger .error( “Destroy method on bean with name ‘” + beanName + “‘ threw an exception” , ex);
}
}
// 触发销毁包含的 bean
// containedBeanMap—bean 的 name 和该 bean 所包含的 bean 的 name 的集合的 map
Set containedBeans = (Set ) this . containedBeanMap .remove(beanName);
if (containedBeans != null ) {
for (Iterator it = containedBeans.iterator(); it.hasNext();) {
String containedBeanName = (String) it.next();
/ 递归调用
destroySingleton(containedBeanName);
}
}
// Remove destroyed bean from other beans’ dependencies.
synchronized ( this . dependentBeanMap ) {
for (Iterator it = this . dependentBeanMap .entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry ) it.next();
Set dependenciesToClean = (Set ) entry.getValue();
dependenciesToClean.remove(beanName);
if (dependenciesToClean.isEmpty()) {
it.remove();
}
}
}
// Remove destroyed bean’s prepared dependency information.
// dependenciesForBeanMap—bean 的 name 和该 bean 的依赖关系的 bean 的 name 集合的 map
this . dependenciesForBeanMap .remove(beanName);
}
====================================================================
本站支持 pay for your wishes