spring mvc web工程里contextConfigLocation 和 classpath

1、如果web.xml不配置contextConfigLocation时,工程会默认加载WEB-INF下的 mvc-dispatcher-servlet.xml文件,如果该目录下没有,启动tomcat时就会报:

Exception

javax.servlet.ServletException: Servlet.init() for servlet [mvc-dispatcher] threw exception
	
Root Cause
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
正确的默认配置:

spring mvc web工程里contextConfigLocation 和 classpath

 

2、如果想指定自己的xml或多个xml时,这个时候就用到了contextConfigLocation.同时要注意引用顺序。

spring mvc web工程里contextConfigLocation 和 classpath