如何使用多个Spring的xml配置文件(多模块配置) (2009-08-22 13:42:43)

 

如何使用多个Spring的xml配置文件(多模块配置)

在用Struts Spring Hibernate进行项目团队开发的时候,我们每个开发人员可能都会用到spring的配置文件,如果我们每个开放人员都用同一个的话,这样会给项目开发带来一定的影响,不过大家不用急,Spring为我们提供了很好的方法,我们可以写多个xml文件,然后将每个文件引入进来就行了,话不多说了,马上开始!!

方法一、在web.xml中定义 contextConfigLocation参数.spring会使用这个参数加载.所有”逗号”分割的xml.如果没有这个参数,spring默认加载WEB-INF/applicationContext.xml文件(我们现在将Spring配置文件都放在WEB-INF下面)

<context-param>     <param-name>contextConfigLocation</param-name>        <param-value>         classpath*:conf/spring/applicationContext_core*.xml,         classpath*:conf/spring/applicationContext_dict*.xml,         classpath*:conf/spring/applicationContext_hibernate.xml,         classpath*:conf/spring/applicationContext_staff*.xml,        classpath*:conf/spring/applicationContext_security.xml        classpath*:conf/spring/applicationContext_modules*.xml        classpath*:conf/spring/applicationContext_cti*.xml       classpath*:conf/spring/applicationContext_apm*.xml    </param-value> </context-param>

contextConfigLocation 参数定义了要装入的 Spring 配置文件。

详细说明如下:利用ServletContextListener 实现。

Spring 提供javax.servlet.ServletContextListener 的一个实现类org.springframeword.web.context.ContextLoaderListener ,该类可以作为listener 使用,它会在创建时自动查找WEB-INF/ 下的applicationContext.xrnl 文件。

因此,如果只有一个配置文件,并且文件名为applicationContext.xml ,则只需在web.xml文件中增加如下代码即可: <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 如果有多个配置文件需要载入,则考虑使用<context-param>即元素来确定配置文件的文件名。由于ContextLoaderListener加载时,会查找名为contextConfigLocation的参数。 因此,配置context-param时参数名字应该是contextConfigLocation。 带多个配置文件的web.xml 文件如下: <1-- XML 文件的文件头二〉 <?xml version="1.0 " encoding="Iso8859-1"?> < 1-- web.xm1 文件的DTD 等信息一〉 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems. 工口c.//DTD Web Application 2.3//EN" ''http://java.sun.com/dtd/web-app_2_3.dtd''> <web-app> <!一确定多个配置文件> <context-param> <1-- 参数名为contextConfigLocation…〉 <param-name>contextConfigLocation</param-name> <!一多个配置文件之间以,隔开二〉 <param-value>/WEB-工NF/daoContext.xml./WEB-INF/application Context.xml</param-value> </context-param>

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

2, 使用匹配符
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext*.xml</param-value> </context-param>

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

3, 如果使用struts加载多个spring配置文件.下面这个配置的其实也是contextConfigLocation变量.

struts-config.xml里面加这个

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation"  value="/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,,,,,,,"/>
4,如果是非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;

最新文章

  1. MongoDB Java Driver操作指南
  2. 【Alpha】Daily Scrum Meeting第五次
  3. iOS 修改UIWebView的UserAgent
  4. Ubuntu下编译第一个C程序的成功运行
  5. Codeforces Round #295 (Div. 2)
  6. vim代码补全-spf13,YouCompleteMe
  7. Java中的浅复制和深复制 Cloneable clone
  8. Git的思想和基本工作原理
  9. OC基础3:一些基本概念
  10. 【C#】开发可以可视化操作的windows服务
  11. centos6.8 安装gitlab记录
  12. mysql进阶(二十二)MySQL错误之Incorrect string value: &#39;\xE7\x81\xAB\xE7\x8B\x90...中文字符输入错误
  13. Python:正则表达式(二):如何使用re.search()返回的匹配对象中的具体内容呢??
  14. 设置光标聚焦输入框(EditText)并弹出软键盘(在适配器中设置)
  15. 创建一个dynamics 365 CRM online plugin (九) - Context.Depth
  16. public class PageRender implements ResponseRender
  17. 可用于在 Microsoft.NET Framework 4.0 中的 ASP.NET 浏览器定义文件的修补程序
  18. unity3d摄像机参数
  19. Windwos在cmd如何复制文本
  20. 爬虫实战2:爬头条网美图--Ajax图片加载处理

热门文章

  1. Comparing Data-Independent Acquisition and Parallel Reaction Monitoring in Their Abilities To Differentiate High-Density Lipoprotein Subclasses 比较DIA和PRM区分高密度脂蛋白亚类的能力 (解读人:陈凌云)
  2. android studio 添加GSON
  3. python基础知识1——简介与入门
  4. Cookie SameSite属性介绍及其在ASP.NET项目中的应用
  5. 阿里云服务器Ubuntu系统搭建LNMP环境
  6. display:flex 简单记录
  7. PyTorch Hub发布!一行代码调用最潮模型,图灵奖得主强推
  8. 给 EF Core 查询增加 With NoLock
  9. ES6中async与await的使用方法
  10. Spring中的设计模式:工厂方法模式