applicationContext.xml位置 路径出错class path resource [applicationContext.xml] cannot be opened

我是基于maven搭建的webapp项目

 

applicationContext.xml位置 路径出错class path resource [applicationContext.xml] cannot be opened_第1张图片

错误提示:

Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
    ... 13 more

 

解决方法:

 

一、把配置文件放到src/main/resources下面

下面三种配置都不行,原因是:用maven构建项目时候resource目录就是默认的classpath

下面在web.xml中配置方法无效,加载配置文件默认 就是WEB-INF/


contextConfigLocation

classpath:applicationContext.xml

 

contextConfigLocation

classpath*:applicationContext.xml

 



  contextConfigLocation
  /WEB-INF/applicationContext.xml

 

 

 

二、加载的时候使用绝对路径定位

同理用FileSystemXmlApplicationContext也可以

ApplicationContext context = new ClassPathXmlApplicationContext("file:/Users/yourname/Desktop/myspring/src/main/webapp/WEB-INF/applicationContext.xml");

 

 

 

 

 

 

 

 

你可能感兴趣的:(Spring)