[Spring] ContextSingletonBeanFactoryLocator

介绍

它将其内部bean工厂引用创建为org.springframework.context.ApplicationContext,而不是SingletonBeanFactoryLocator的简单BeanFactory

如何使用

       BeanFactoryLocator beanFactoryLocator = ContextSingletonBeanFactoryLocator.getInstance("classPath");
       BeanFactoryReference  refer = beanFactoryLocator.useBeanFactory("id of bean");
       BeanFactory context = refer.getFactory();

源码分析

ContextSingletonBeanFactoryLocator#getInstance

public static BeanFactoryLocator getInstance(String selector) throws BeansException {
        // 定位文件的路径
              String resourceLocation = selector;
        if (resourceLocation == null) {
                        // 见下面默认路径
            resourceLocation = DEFAULT_RESOURCE_LOCATION;
        }

        // For backwards compatibility, we prepend "classpath*:" to the selector name if there
        // is no other prefix (i.e. "classpath*:", "classpath:", or some URL prefix).
        if (!ResourcePatternUtils.isUrl(resourceLocation)) {
            resourceLocation = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resourceLocation;
        }
                
                // 下面是创建
        synchronized (instances) {
            if (logger.isTraceEnabled()) {
                logger.trace("ContextSingletonBeanFactoryLocator.getInstance(): instances.hashCode=" +
                        instances.hashCode() + ", instances=" + instances);
            }
            BeanFactoryLocator bfl = instances.get(resourceLocation);
            if (bfl == null) {
                // 重点看下这里 见下面创建对象
                bfl = new ContextSingletonBeanFactoryLocator(resourceLocation);
                instances.put(resourceLocation, bfl);
            }
            return bfl;
        }
    }

默认路径

private static final String DEFAULT_RESOURCE_LOCATION = "classpath*:beanRefContext.xml";

缓存对象

private static final Map<String, BeanFactoryLocator> instances = new HashMap<String, BeanFactoryLocator>();

创建对象

protected ContextSingletonBeanFactoryLocator(String resourceLocation) {
        super(resourceLocation);
    }

这里直接调用super对象,也就是SingletonBeanFactoryLocator

下面我们重点说下SingletonBeanFactoryLocator

SingletonBeanFactoryLocator

实现接口

BeanFactoryLocator,里面只有一个行为

BeanFactoryReference useBeanFactory(String factoryKey) throws BeansException;

这个类就是查找和使用定义的工厂

SingletonBeanFactoryLocator的使用方式

 <beans>
  <bean id="com.mycompany.myapp"
           class="org.springframework.context.support.ClassPathXmlApplicationContext">
       <constructor-arg>
         <list>
           <value>com/mycompany/myapp/util/applicationContext.xml</value>
           <value>com/mycompany/myapp/dataaccess/applicationContext.xml</value>
           <value>com/mycompany/myapp/dataaccess/services.xml</value>
         </list>
       </constructor-arg>
     </bean>
   </beans>

使用方式
BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance();
   BeanFactoryReference bf = bfl.useBeanFactory("com.mycompany.myapp");
   // now use some bean from factory
   MyClass zed = bf.getFactory().getBean("mybean");

主要看下useBeanFactory的方法

@Override
    public BeanFactoryReference useBeanFactory(String factoryKey) throws BeansException {
        synchronized (this.bfgInstancesByKey) {
            BeanFactoryGroup bfg = this.bfgInstancesByKey.get(this.resourceLocation);

            if (bfg != null) {
                bfg.refCount++;
            }
            else {
                // This group definition doesn't exist, we need to try to load it.
                if (logger.isTraceEnabled()) {
                    logger.trace("Factory group with resource name [" + this.resourceLocation +
                            "] requested. Creating new instance.");
                }

                // 重点看下这里
                BeanFactory groupContext = createDefinition(this.resourceLocation, factoryKey);

                // Record its existence now, before instantiating any singletons.
                bfg = new BeanFactoryGroup();
                bfg.definition = groupContext;
                bfg.refCount = 1;
                this.bfgInstancesByKey.put(this.resourceLocation, bfg);
                this.bfgInstancesByObj.put(groupContext, bfg);

                // Now initialize the BeanFactory. This may cause a re-entrant invocation
                // of this method, but since we've already added the BeanFactory to our
                // mappings, the next time it will be found and simply have its
                // reference count incremented.
                try {
                    initializeDefinition(groupContext);
                }
                catch (BeansException ex) {
                    this.bfgInstancesByKey.remove(this.resourceLocation);
                    this.bfgInstancesByObj.remove(groupContext);
                    throw new BootstrapException("Unable to initialize group definition. " +
                            "Group resource name [" + this.resourceLocation + "], factory key [" + factoryKey + "]", ex);
                }
            }

            try {
                BeanFactory beanFactory;
                if (factoryKey != null) {
                    beanFactory = bfg.definition.getBean(factoryKey, BeanFactory.class);
                }
                else {
                    beanFactory = bfg.definition.getBean(BeanFactory.class);
                }
                return new CountingBeanFactoryReference(beanFactory, bfg.definition);
            }
            catch (BeansException ex) {
                throw new BootstrapException("Unable to return specified BeanFactory instance: factory key [" +
                        factoryKey + "], from group with resource name [" + this.resourceLocation + "]", ex);
            }

        }
    }
protected BeanFactory createDefinition(String resourceLocation, String factoryKey) {
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();

        try {
            Resource[] configResources = resourcePatternResolver.getResources(resourceLocation);
            if (configResources.length == 0) {
                throw new FatalBeanException("Unable to find resource for specified definition. " +
                        "Group resource name [" + this.resourceLocation + "], factory key [" + factoryKey + "]");
            }
            reader.loadBeanDefinitions(configResources);
        }
        catch (IOException ex) {
            throw new BeanDefinitionStoreException(
                    "Error accessing bean definition resource [" + this.resourceLocation + "]", ex);
        }
        catch (BeanDefinitionStoreException ex) {
            throw new FatalBeanException("Unable to load group definition: " +
                    "group resource name [" + this.resourceLocation + "], factory key [" + factoryKey + "]", ex);
        }

        return factory;
    }

这里说明下,其实还是我们编程式的创建DefaultListableBeanFactory,这里就没啥好讲的,

总结

如果不是web工程使用spring的话,可以使用这种方式
遗憾的是spring5 已经去掉了这种机制:https://jira.spring.io/browse/SPR-15154

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文是我自己在秋招复习时的读书笔记,整理的知识点,也是为了防止忘记,尊重劳动成果,转载注明出处哦!如果你也喜欢,那...
    波波波先森阅读 12,328评论 6 86
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,970评论 6 342
  • 1.1 spring IoC容器和beans的简介 Spring 框架的最核心基础的功能是IoC(控制反转)容器,...
    simoscode阅读 6,751评论 2 22
  • 今天周末,我加入到日更这个组织了,现在趁佳妹看电视,我来码几个字。 今天可以吃多了,而且吃的晚,所以不能出...
    沉默中进步阅读 251评论 0 0