web配置文件与Spring详解

在web.xml中定义contentConfigLocation参数,Spring会使用这个参数加载所有逗号分隔的.xml文件,Spring默认加载web-INF下的applicationContext.xml文件.

<content-param>
     <param-name>contentConfigLocation</param-name>
     <paramvalue>classpath*:config/applicationContext.xml,classpath*:config/applicationContext-shiro.xml
     </param-value>
</content-param>

    contencConfigLocation参数定义了要装入Spring的配置文件,装入的原理说明如下:

Sping中提供ServletContentListener的一个实现类contentLoaderListener,该类可以做一个Listener使用,如果要装入的配置文件只用一个并且名称为applicationContent.xml时,则只需要在文件中加入以下代码即可:

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 如果要装入的配置文件有多个时,则需要使用<content-param>确定文件名称,由于contentLoaderListener加载时会查找名为contentConfigLocation的参数,因此,在定义<content-param>的名称时使用contentConfigLocation.

有多个配置文件如下配置:

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
		classpath*:config/applicationContext.xml,classpath*:config/applicationContext-shiro.xml
		</param-value>
  </context-param>
  <context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>EPMAS.webapp.root</param-value>
  </context-param>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

 如果没有contentConfigLocation参数指定的配置文件,则Spring会自动的查找applicationContent.xml配置文件;如果配置有contentConfigLocation参数指定的配置文件,该参数指定的是一个字符串,Spring的contentLoaderListener负责将该字符串分解成多个配置文件,逗号,空格,分号多可以作为分隔符;如果没有contentConfigLocation参数指定为配置文件,也没有名称为applicationContent.xml的配置文件,都会导致Spring无法加载配置文件或者创建applicationContent实例异常.

       注意:使用contentLoaderListener类来实现配置文件的装入仅适用于适用于Servlet2.4及以上规范Servlet容器。

基于contentLoaderServlet类来实现Spring配置文件的装入可以适用于servlet2.3以下规范的servlet容器: 

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
			classpath*:config/applicationContext*.xml
		</param-value>
  </context-param>
<servlet>
   <servlet-name>context</servlet-name>
     <servletclass>
        org.springframework.web.context.ContextLoaderServlet
     </servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

 Spring 提供了一个特殊的Servllet 类: ContextLoaderServlet。该Servlet 在启动时,会自动查找WEB-INF下的applicationContext. xml 文件。当然,为了让ContextLoaderServlet 随应用启动而启动,应将此Servlet 配置成load-on-startup 的Servleto load-on-startup 的值小一点比较合适,因为要保证ApplicationContext 优先创建。 

从servlet容器启动时加载组件的顺序来看(ServletContext-> listener ->filter -> servlet),Listener组件是优先于Servlet组件的。基于Servlet方式的加载方案主要是为了兼容Servlet2.3及以下规范的Servlet容器。以Tomcat为例,Tomcat 5.x都已经支持Servlet2.4规范了,因此,基于Listener方式启动Spring容器是目前的主流选择。

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值