第一:加载web.xml之后,读哪个配置文件问题?即是applicationContext.xml还是xxx-servlet.xml?
1 <servlet> 2 <servlet-name>dispatch</servlet-name> 3 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 4 <init-param> 5 <param-name>contextConfigLocation</param-name> 6 <param-value>/WEB-INF/applicationContext.xml</param-value> 7 </init-param> 8 </servlet>
①:在没有<init-param>时,会自动寻找xxx-servlet.xml(默认路径为/WEN-INF/xxx-servlet.xml)(xxx=定义的servlet-name 即<servlet-name>dispatch</servlet-name>)
即是:dispatch-servlet.xml
②:如果有<init-param>,就会寻找<init-param>中配置的文件,不会再找xxx-servlet.xml
第二:如果配置了<init-param>,那么applicationContext.xml文件在哪里?
解答:下面第一种方式,在/WebContent/WEB-INF/目录下
第二种方式,在/src/目录下
<servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>dispatch</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>
</servlet>