SpringMVC啓動過程當中若是你有認真觀察的話,你會發現控制檯將地址映射的信息打印一遍,而且提醒你,系統已經啓動完畢。若是此時你的系統又再次打印了一遍地址映射的信息,並再次提醒你係統啓動關閉,那你可就要注意了,你可能存在着地址映射兩次的問題。web
首先咱們來看一段XML來分析一下,爲何會有這個問題的存在。redis
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param>
上面這個配置有沒有很眼熟,若是沒有看過的話,你Spring還沒學吧,小夥子。。。。spring
你們是否是都習慣性的命名,而後經過掃描一遍掃進去,好比說我:app
applicationContext.xmlspa
applicationContext-shiro.xmlcode
applicationContext-redis.xmlxml
applicationContext-servlet.xmlservlet
首先咱們的ContextLoaderListener會根據contextConfigLocation的配置信息去查找相關的配置文件來啓動Sping容器,很幸運的是咱們配置的值爲classpath:applicationContext*.xml,這個配置意味着咱們在classpath下配置的全部的配置文件都將會被掃描到Spring容器中(PS:classpath沒什麼好解釋了的吧)。緊接着,全部的配置文件所有被加載成功了,包括SpringMVC在內的applicationContext-servlet.xml配置文件,Spring容器啓動完畢了,此時SpringMVC的地址第一次被映射。it
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
啓動完畢了之後,系統還發現你在web.xml中配置瞭如上信息,你的這個配置,就至關於告訴系統,我要開啓SpringMVC功能,DispatcherServlet會根據contextConfigLocation的配置進行加載SpringMVC的相關配置信息,此時SpringMVC的地址第二次被映射。io
緣由很簡單,其實就是Spring容器ContextLoaderListener加載了一遍,DispatcherServlet緊接着又加載了一遍, 這就是爲何會映射兩遍的緣由,解決方案很簡單,就是把你的強迫症改掉,把SpringMVC的配置文件的名字改掉,再也不被ContextLoaderListener的contextConfigLocation所匹配到就能夠了。