码迷,mamicode.com
首页 > 其他好文 > 详细

BeanFactory not initialized or already closed exception

时间:2017-05-17 23:49:07      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:spring

启动spring 报错:
BeanFactory not initialized or already closed call ‘refresh‘ before accessing beans via the ApplicationContext


此错误是由AbstractRefreshableApplicationContext#getBeanFactory方法中抛出来的.
@Override
public final ConfigurableListableBeanFactory getBeanFactory() {
   synchronized (this.beanFactoryMonitor) {
      if (this.beanFactory == null) {
         throw new IllegalStateException("BeanFactory not initialized or already closed - " +
               "call ‘refresh‘ before accessing beans via the ApplicationContext");
      }
      return this.beanFactory;
   }
}

是因为在Spring在解析xml时出错, 导致close方法被调用, 而在close方法调用链上包括getBeanFactory.


详解:
方法一:
protected final void closeBeanFactory() {
   ...
   this.beanFactory = null;
}

方法二:
protected final void refreshBeanFactory() throws BeansException {
   ...
   closeBeanFactory();
   DefaultListableBeanFactory beanFactory = createBeanFactory();
   ...
   loadBeanDefinitions(beanFactory); // 加载bean定义(解析xml文件)
   ...
   this.beanFactory = beanFactory;
}

由方法一,二可知: 当加载bean定义(解析xml文件)出错时, AbstractRefreshableApplicationContext中的变量beanFactory为null.

方法三:
public void close() {
   ...
   doClose();
   ...
}
方法四:
protected void doClose() {
   ...
   destroyBeans();
   ...
}
方法五:
protected void destroyBeans() {
   getBeanFactory().destroySingletons();
}

由方法三,四,五可知: ApplicationContext的close方法被调用时,会调用getBeanFactory(), 这时beanFactory为null.


close最终是被ContextLoaderListener#contextDestroyed所调用.


BeanFactory not initialized or already closed exception

标签:spring

原文地址:http://xnatural.blog.51cto.com/6103484/1926781

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!