http://blog.csdn.net/wlwlwlwl015/article/details/48134763
Maven 项目的 classpath 理解
applicationContext.xml放在maven的src/main/resources下,将web.xml中的<param-value>配置如下。
<servlet> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet>
如下异常:Could not open ServletContext resource [/WEB-INF/applicationContext.xml
原因:如果不特意指定参数名为contextConfigLoction的<context-parameter>元素,那么spring的ContextLoderListener监听器就会在/WEB-INF/下去寻找并加载该目录下的名为applicationContext.xml这个文件。
解决:
1 <context-param> 2 <param-name>contextConfigLocation</param-name> 3 <param-value>classpath:applicationContext.xml</param-value> 4 </context-param> 5 6 <listener> 7 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 8 </listener>