在web.xml中通配置contextConfigLocation参数来配置Spring
多个配置文件的加载
<!--第一配置参数文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:conf/spring/applicationContext_modules*.xml
classpath*:conf/spring/applicationContext_cti*.xml
classpath*:conf/spring/applicationContext_apm*.xml
</param-value>
</context-param>
<!--第二步配置启动是需要的监听器和servlet之一-->
<!--监听器-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
其中ContextLoaderListener这个类的作用就是在web容器启动时配置ApplicationContext需要的信息,这个类实现了ServletContextListener这个接口,启动容器时,就会默认执行它实现的方法,
<!--通过自启动的servlet来启动-->
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup><!--这里来设置servlet的启动顺序-->
</servlet>
其中对于ContextLoaderListener和ContextLoaderServlet来说两者实现 的功能都 是一样的,不同之处在于ContextLoaderListener不能在与Servlet 2.2兼容的web容器中使用。根据Servlet 2.4规范, servlet context listener要在web应用程序的servlet context建立后立即执行,并要能够响应第一个请求(在servlet context要关闭时也一样):这样一个servlet context listener是初始化Spring ApplicationContext的理想场所。虽然使用哪个完全取决于你,但是在同等条件下应该首选ContextLoaderListener