在使用Spring框架的时候,若是咱们使用的是XML文件配置Bean的方式的话,咱们每每会在web.xml里面配置以下内容:web
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/spring-bean-config.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
这是由于若是咱们有配置ContextLoaderListener, Spring会去web.xml中看咱们是否有定义contextConfigLocation这个参数,若是有则Spring容器(Bean工厂)会把定义在该xml文件中的bean加载到容器中,那若是没有定义contextConfigLocation参数,Spring会怎么办?web服务器启动的时候会不会报错呢?spring
Spring有一个概念就是约定优于配置,也就是说,即便你没有显示定义xml文件的位置,Spring容器会到一个约定的地方去找该文件,若是找不到就要报FileNotFoundException了,咱们来看看下面代码段:服务器
"/WEB-INF/applicationContext.xml" 就是默认的文件地址了,若是你使用了Spring MVC,你确定要到web.xml里面定义DispatcherServletapp
<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet>
这个时候若是你没有显示配置contextConfigLocation参数的话,Spring会到该默认路径下加载配置文件"WEB-INF/dispatcherServlet-servlet.xml", 若是不存在就报异常。框架
以上源码存在于XmlWebApplicationContext.spa