1. 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的配置文件
2. 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目录下,所以没找到配置文件。
也可以这么做
举个例子:ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
这里的classpath 是src的根目录 spring是一个文件夹 applicationContext-*.xml 是spring容器初始化的xml文件