博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(转)web.xml中的contextConfigLocation在spring中的作用
阅读量:4673 次
发布时间:2019-06-09

本文共 3305 字,大约阅读时间需要 11 分钟。

 

一、Spring如何使用多个xml配置文件

  1、在web.xml中定义contextConfigLocation参数,Spring会使用这个参数去加载所有逗号分隔的xml文件,如果没有这个参数,Spring默认加载web-inf/applicationContext.xml文件。

  例如:

contextConfigLocation
classpath*:conf/spring/applicationContext_core*.xml, classpath*:conf/spring/applicationContext_dict*.xml, classpath*:conf/spring/applicationContext_hibernate.xml,

  contextConfigLocation 参数的<param-value>定义了要装入的 Spring 配置文件。

原理:利用ServletContextListener 实现

  Spring 提供ServletContextListener 的一个实现类ContextLoaderListener ,该类可以作为listener 使用,它会在创建时自动查找WEB-INF/ 下的applicationContext.xrnl 文件。因此,如果只有一个配置文件,并且文件名为applicationContext.xml ,则只需在web.xml文件中增加如下代码即可:

    
org.springframework.web.context.ContextLoaderListener

  如果有多个配置文件需要载入,则考虑使用<context-param>元素来确定配置文件的文件名。由于ContextLoaderListener加载时,会查找名为contextConfigLocation的参数。因此,配置<context-param>时参数名字应该是contextConfigLocation。

  带多个配置文件的web.xml 文件如下:

<1-- XML 文件的文件头二〉
  
  
    <1-- 参数名为contextConfigLocation…〉    
contextConfigLocation
    
/WEB-工NF/daoContext.xml./WEB-INF/applicationContext.xml  
  
  
    
org.springframework.web.context.ContextLoaderListener
  

  如果没有contextConfigLocation 指定配置文件,则Spring 自动查找applicationContext.xml 配置文件。如果有contextConfigLocation,则利用该参数确定的配置文件。该参数指定的一个字符串,Spring 的ContextLoaderListener 负责将该字符串分解成多个配置文件,逗号","、空格" "及分号";"都可作为字符串的分割符。如果既没有applicationContext.xml 文件,也没有使用contextConfigLocation参数确定配置文件,或者contextConfigLocation确定的配置文件不存在。都将导致Spring 无法加载配置文件或无法正常创建ApplicationContext 实例

 

配置一个spring为加载而设置的servlet可以达到同样效果.

  采用load-on-startup Servlet 实现。

  Spring 提供了一个特殊的Servllet 类: ContextLoaderServlet。该Servlet 在启动时,会自动查找WEB-IN日下的applicationContext. xml 文件。当然,为了让ContextLoaderServlet 随应用启动而启动,应将此Servlet 配置成load-on-startup 的Servleto load-on-startup 的值小一点比较合适,因为要保证ApplicationContext 优先创建。如果只有一个配置文件,并且文件名为applicationContext. xml ,则在web.xml 文件中增加如下代码即可:

  
context    
org.springframework.web.context.ContextLoaderServlet
  
l

  带多个配置文件的web.xml 文件如下:

  <'一确定多个配置文件一>  
    
    
contextConfigLocation
    

 

二、使用匹配符

  
contextConfigLocation
  
/WEB-INF/applicationContext*.xml

  比如说用到Hibernate,则把hibernate相关的配置放在applicationContext-hibernate.xml这一个文件,而一些全局相关的信息则放在applicationContext.xml,其他的配置类似.这样就可以加载了,不必写用空格或是逗号分开!

 

三、如果使用Struts加载多个Spring配置文件

  下面这个配置的其实也是contextConfigLocation变量.struts-config.xml里面加这个

  

 

四、如果是非j2ee应用程序加载

ApplicationContext act = new ClassPathXmlApplicationContext(new String[]{"bean1.xml","bean2.xml"});BeanDefinitionRegistry reg = new DefaultListableBeanFactory();XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(reg);reader.loadBeanDefinitions(new ClassPathResource("bean1.xml"));reader.loadBeanDefinitions(new ClassPathResource("bean2.xml"));BeanFactory bf = (BeanFactory)reg;

转载于:https://www.cnblogs.com/Jeely/p/10762040.html

你可能感兴趣的文章
Number and String
查看>>
java中的值传递和引用传递2<原文:http://blog.csdn.net/niuniu20008/article/details/2953785>...
查看>>
css实现背景图片模糊
查看>>
什么是runtime?什么是webgl?
查看>>
秋季学习总结
查看>>
categorical_crossentropy VS. sparse_categorical_crossentropy
查看>>
强引用,弱引用,4种Java引用浅解(涉及jvm垃圾回收)
查看>>
多线程如何确定线程数
查看>>
UGUI RectTransform
查看>>
学前班
查看>>
手把手教您扩展虚拟内存
查看>>
android-samples-mvp
查看>>
oracle 11g r2安装
查看>>
关于自关联1
查看>>
存储控制器、MMU、flash控制器介绍
查看>>
hdu-1814(2-sat)
查看>>
自我反省
查看>>
反射,得到Type引用的三种方式
查看>>
pl sql练习(2)
查看>>
Problem B: 判断回文字符串
查看>>