下面直接如主题,下面是web.xml里面很常见的一段配置:
<servlet> <servlet-name>springServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/xxxx1.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
如果在实际配置中没有标红部分配置,那么spring框架会查找的contextConfigLocation名字为getDefaultConfigLocations返回的值,下面直接看这个方法:
/** * The default location for the root context is "/WEB-INF/applicationContext.xml", * and "/WEB-INF/test-servlet.xml" for a context with the namespace "test-servlet" * (like for a DispatcherServlet instance with the servlet-name "test"). */ @Override protected String[] getDefaultConfigLocations() { if (getNamespace() != null) { return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};
//DEFAULT_CONFIG_LOCATION_PREFIX =/WEB-INF/ ,DEFAULT_CONFIG_LOCATION_SUFFIX=.xml } else { return new String[] {DEFAULT_CONFIG_LOCATION}; } }
类FrameworkServlet相关部分代码
public String getNamespace() { return (this.namespace != null ? this.namespace : getServletName() + DEFAULT_NAMESPACE_SUFFIX); //DEFAULT_NAMESPACE_SUFFIX为-servlet }
也就是说如果没有上面标红部分配置,spring框架会寻找的servlet config文件为/WEB-INF/servlet名字-servlet.xml