这个'外部'BeanFactory的目的是创建并保存一个或多个'内部'BeanFactory或ApplicationContext实例的副本,并允许直接或通过别名获取这些实例。
public class SingletonBeanFactoryLocator implements BeanFactoryLocator {
private static final String DEFAULT_RESOURCE_LOCATION = "classpath*:beanRefFactory.xml";//默认配置文件地址
protected static final Log logger = LogFactory.getLog(SingletonBeanFactoryLocator.class);//日志
private static final Map<String, BeanFactoryLocator> instances = new HashMap();
private final Map<String, SingletonBeanFactoryLocator.BeanFactoryGroup> bfgInstancesByKey = new HashMap();//通过string(即resourceLocation)找到beanfactory
private final Map<BeanFactory, SingletonBeanFactoryLocator.BeanFactoryGroup> bfgInstancesByObj = new HashMap();//通过beanfactory找到beanfactory
private final String resourceLocation;//资源位置
//静态内部类
private static class BeanFactoryGroup {
private BeanFactory definition;
private int refCount;
private BeanFactoryGroup() {
this.refCount = 0;
}
}
//内部类
private class CountingBeanFactoryReference implements BeanFactoryReference {
private BeanFactory beanFactory;
private BeanFactory groupContextRef;
}
/**
通过string 找到beanfactory.如果不存在则创建一个factory.
*/
public BeanFactoryReference useBeanFactory(String factoryKey) throws BeansException {
synchronized(this.bfgInstancesByKey) {
SingletonBeanFactoryLocator.BeanFactoryGroup bfg = (SingletonBeanFactoryLocator.BeanFactoryGroup)this.bfgInstancesByKey.get(this.resourceLocation);//加载配置文件,解析配置文件,初始化一个BeanFactoryGroup
BeanFactory beanFactory;
//判断创建BeanFactoryGroup成功没有
if (bfg != null) {
bfg.refCount++;
} else {
if (logger.isTraceEnabled()) {
logger.trace("Factory group with resource name [" + this.resourceLocation + "] requested. Creating new instance.");
}
//新建一个beanfactory,beanfactorygroup。并且设置definition和refcount
beanFactory = this.createDefinition(this.resourceLocation, factoryKey);
bfg = new SingletonBeanFactoryLocator.BeanFactoryGroup();
bfg.definition = beanFactory;
bfg.refCount = 1;
//把beanfactorygroup加入到bfgInstancesByKey,bfgInstancesByObj两个map集合中
this.bfgInstancesByKey.put(this.resourceLocation, bfg);
this.bfgInstancesByObj.put(beanFactory, bfg);
try {
this.initializeDefinition(beanFactory);
} catch (BeansException var8) {
this.bfgInstancesByKey.remove(this.resourceLocation);
this.bfgInstancesByObj.remove(beanFactory);
throw new BootstrapException("Unable to initialize group definition. Group resource name [" + this.resourceLocation + "], factory key [" + factoryKey + "]", var8);
}
}
SingletonBeanFactoryLocator.CountingBeanFactoryReference var10000;
try {
if (factoryKey != null) {
beanFactory = (BeanFactory)bfg.definition.getBean(factoryKey, BeanFactory.class);
} else {
beanFactory = (BeanFactory)bfg.definition.getBean(BeanFactory.class);
}
//建立一个CountingBeanFactoryReference对象并返回
var10000 = new SingletonBeanFactoryLocator.CountingBeanFactoryReference(beanFactory, bfg.definition);
} catch (BeansException var7) {
throw new BootstrapException("Unable to return specified BeanFactory instance: factory key [" + factoryKey + "], from group with resource name [" + this.resourceLocation + "]", var7);
}
return var10000;
}
}
}