SingletonBeanFactoryLocator

这个'外部'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;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值