Caused by: java.io.FileNotFoundException:... cannot be opened because it does not exist


初学Spring,配置了applicationContext.xml后,测试时出现了这样的错误:

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		PersonBean bean = (PersonBean)ctx.getBean("userBean");
		
		bean.show();

Caused by: java.io.FileNotFoundException: class path resource [com/bless/common/spring/springApplicationContext.xml] cannot be opened because it does not exist


这应该是编译路径的问题;

使用new ClassPathXmlApplicationContext("applicationContext.xml"); 来读取时,将applicationContext.xml放到/WEB-INF/classes目录下;
或放到src目录下,

// ClassPathXmlApplicationContext 是读取 src 目录下的配置文件
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");

//  FileSystemXmlApplicationContext 即系统文件路径,文件的目录。
ApplicationContext context = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章