BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the Applic

在网上找的时候,很多人说是因为

错误写法:ApplicationContext ctx = new ClassPathXmlApplicationContext();

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

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

正确写法:ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");


但是我使用的是springmvc框架:后来发现问题在于在不同的包结构下存在相同名字的controller。导致Spring实例化BeanFactory失败。如果@Controller里面的value你没有设置的话,spring会按照你的class名称去实例化属于它的bean,当出现两个相同的class名称的时候就会报错。

BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the Applic