美文网首页
obtainFreshBeanFactory()

obtainFreshBeanFactory()

作者: 轻易流逝 | 来源:发表于2018-11-05 17:16 被阅读0次

    方法内容:

    protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
            refreshBeanFactory();
            ConfigurableListableBeanFactory beanFactory = getBeanFactory();
            if (logger.isDebugEnabled()) {
                logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
            }
            return beanFactory;
        }
    

    实现一:org.springframework.context.support.AbstractRefreshableApplicationContext.class

    refreshBeanFactor()代码
    @Override
        protected final void refreshBeanFactory() throws BeansException {
            //如果有beanFactory先销毁
            if (hasBeanFactory()) {
                destroyBeans();
                closeBeanFactory();
            }
            try {
                DefaultListableBeanFactory beanFactory = createBeanFactory();//创建新工厂
                beanFactory.setSerializationId(getId());//设置serializationId
                customizeBeanFactory(beanFactory);//设置beanFactory
                loadBeanDefinitions(beanFactory);//加载BeanDefinitions
                synchronized (this.beanFactoryMonitor) {
                    this.beanFactory = beanFactory;
                }
            }
            catch (IOException ex) {
                throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
            }
        }
    

    1、先销毁原有beanFactory;
    2、创建并初始化BeanFactory;
    3、加载BeanDefinitions。

    实现二:org.springframework.context.support.GenericApplicationContext.class

    refreshBeanFactor()代码

    @Override
        protected final void refreshBeanFactory() throws IllegalStateException {
            if (!this.refreshed.compareAndSet(false, true)) {
                throw new IllegalStateException(
                        "GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once");
            }
            this.beanFactory.setSerializationId(getId());
        }
    

    仅设置beanFactory的serializationId;

    那么,这个beanFactory是在什么时候实例化的?beanDefinitions是在什么地方加载的?

    beanFactory实例化:
    beanFactory是在GenericApplicationContext的默认构造函数里实例化的(DefaultListableBeanFactory

    加载beanDefinitions:
    GenericApplicationContext的子类在构造函数里加载beanDefinitions

    相关文章

      网友评论

          本文标题:obtainFreshBeanFactory()

          本文链接:https://www.haomeiwen.com/subject/jaegzftx.html