在使用spring时,遇到几个异常:
第一个异常:
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
解决:
在buildpath中加入commons-logging-1.1.3.jar
第二个异常:
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
解决:
错误的写法是:ApplicationContext ctx = new ClassPathXmlApplicationContext();
应修改为ApplicationContext ctx = new ClassPathXmlApplicationContext(“beans.xml”);
其中beans.xml是spring bean的配置文件
修改上一个异常之后,第三个异常:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist
解决:
是因为beans.xml没有放在src目录下,所以没找到配置文件。