BeanFactory not initialized or already closed exception

启动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所调用.
时间: 2024-08-08 13:54:07

BeanFactory not initialized or already closed exception的相关文章

BeanFactory not initialized or already closed - call 'refresh' before accessing beans解决办法

今天在写Spring程序时遇到了一个很常见的错误,而我以前好像一直没碰到过,今天才见到这个错误,经过研究解决了这个错误,犯这个错误真是不应该啊. log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http

BeanFactory not initialized or already closed - call 'refresh' before access

Exception in thread "main" java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext 遇到这种错误的时候,看到网上很多人都是建议这么解决: BeanFactory没有实例化或者已经关闭了,其实产生错误的原因很简单,在写: Appl

解决java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext这个问题

今天在运行别人的SSH项目时,遇到了这个问题 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate compon

java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

spring的项目中有时候会报错:java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext 原因:spring初始化bean对象出错:1)xml的bean的id 有重复: 2)如果是注解配置:可能是注解的名称有重复: 只需要将重复的bean 的id和重复的注解改了就

解决springjava.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh'异常

错误写法: cxt = new ClassPathXmlApplicationContext(); productService = (ProductService)cxt.getBean("productTypeServiceBean"); 正确写法: cxt = new ClassPathXmlApplicationContext("beans.xml"); productService = (ProductService)cxt.getBean("p

spring项目启动报错BeanFactory not initialized or already closed

spring项目启动的时候报如下错误: java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContextat org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanF

Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener

配置spring的时候xml加载到 <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/> 时报错: 2015-9-18 21:16:39 org.apache.catalina.core.StandardContext listenerStart 严重: Exception sending context initialized

nested exception is java.io.FileNotFoundException: Could not open ServletContext resource

<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> 注意这段配置尽量写在靠前的位置,因为在tomcat启动后web项目会先初始化上下文,我们这段配置正是指定了上下文配置的文件位置,在上下文初始化完成之后才应当继

严重: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener java.lang.IllegalStateException

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListenerorg.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext re