我是基于maven搭建的webapp项目
错误提示:
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/
<!-- 此方法配置无效 classpath:无法访问到WEB-INF下-->
<context-param><param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 此方法配置无效 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!--此方法也不行 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param>
二、加载的时候使用绝对路径定位
同理用FileSystemXmlApplicationContext也可以
ApplicationContext context = new ClassPathXmlApplicationContext("file:/Users/yourname/Desktop/myspring/src/main/webapp/WEB-INF/applicationContext.xml");