学Spring之web.xml中的contextConfigLocation的作用

在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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值