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

Posted 程序波

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.IllegalStateException: BeanFactory not initialized or already closed - call ‘refresh‘ before accessing beans via the ApplicationContext
    at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:172)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1097)
    at com.pb.demo.Test.main(Test.java:12)
技术分享

 

BeanFactory没有实例化或者已经关闭了,其实产生错误的原因很简单,在写:

ApplicationContext ctx = new ClassPathXmlApplicationContext();

时,没有指定配置文件,Spring实例化BeanFactory的时候是默认到classPath下面查找名为applicationContext.xml的文件的,但是呢,你又没有指定,所以出现了这个错误。

这就是错误的原因,在括号写上配置文件名就行了。

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

这样的话就不会出现这样的错误了。

以上是关于BeanFactory not initialized or already closed - call 'refresh' before accessing beans解决办法(示例的主要内容,如果未能解决你的问题,请参考以下文章

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

求助错误,BeanFactory not initialized or already closed

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

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

java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh&#

java.lang.IllegalStateException: BeanFactory not initialized or already closed - call ‘refresh‘ befo