`
bei-jin-520
  • 浏览: 108599 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

spring如何使用多个xml配置文件

阅读更多

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

<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 提供ServletContextListener 的一个实现类ContextLoaderListener ,该类可以作
为listener 使用,它会在创建时自动查找WEB-INF/ 下的applicationContext.xrnl 文件。因
此,如果只有一个配置文件,并且文件名为applicationContext.xml ,则只需在web.xml
文件中增加如下代码即可:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果有多个配置文件需要载入,则考虑使用<context-para即元素来确定配置文件的
文件名。由于ContextLoaderListener加载时,会查找名为contextConfigLocation的参数。
因此,配置context-param时参数名字应该是contextConfigLocation。
带多个配置文件的web.xml 文件如下:
<1-- XML 文件的文件头二〉
<?xml version="l.O" encoding="工80-8859-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创建Applicat工onContext 实例-->
<listener>
<listener-class>org.spr工ngframework.web.context.ContextLoader
Listener</listener-class>
</listener>
</web-app>
如果没有contextConfigLocation 指定配置文件,则Spring 自动查找application
Context. xrnl 配置文件。如果有contextConfigLocation,则利用该参数确定的配置文件。
该参数指定的一个字符串, Spring 的ContextLoaderListener 负责将该字符串分解成多个
配置文件,逗号","、空格" "及分号";"都可作为字符串的分割符。
如果既没有applicationContext. xrnl 文件,也没有使用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 的值小一点比较合适,因为要保证Application
Context 优先创建。如果只有一个配置文件,并且文件名为applicationContext. xml ,则在
web.xml 文件中增加如下代码即可:
<servlet>
<servlet-name>context</servlet口-arne>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</
servlet-class>
<load-on-startup>l</load-o 口-startup>
</servlet>

。带多个配置文件的web且nl 文件如下:
<!-- XML 文件的文件头-->
<?xml version="1.0" encoding="工SO-8859-1"?>
<! -- web.xml 文件的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>
<!-- 参数名为contextConfigLocation-->
<param-name>contextConfigLocation</param-name>
<!-- 多个配置文件之间以,隔开一〉
<param-value>/WEB-工NF/daoContext.xml, !WEB-工NF/applicationContext.
xml</param-value>
</context-param>
<!一采用load-on-startup Servlet 创建Applicat工onContext 实例一〉
<servlet>
<servlet-narne>context</servlet-narne>
<servlet-class>org.springframework.web.context.ContextLoader
Servlet</servlet-class>
<!一下面值小一点比较合适,会优先加载一〉
<load-on-startup>l</load-on-startup>
</servlet>
</web-app>


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;
文章出处http://zhangxinzhou.blog.ccidnet.com/blog-htm-do-showone-uid-36421-itemid-976755-type-blog.html

分享到:
评论

相关推荐

    Spring使用XML配置声明式事务

    Spring使用XML配置声明式事务 ,具体效果和过程看博文 http://blog.csdn.net/evankaka/article/details/45478007

    Spring Boot多模块配置文件读取

    我们使用Spring Boot编写多个模块开发时,我们希望各个模块使用各自的配置文件,避免将整个项目的配置文件都写在一起,从而降低各个模块之间的耦合度。

    logback-spring.xml文件配置

    logback-spring.xml文件配置,1、异步日志,2、滚动日志,存放固定时长的日志,超过时间的自动删除,3、单个文件超过指定大小,分成多个,防止单个文件过大,查看不方便

    在Spring Boot中加载XML配置的完整步骤

    主要给大家介绍了关于在Spring Boot中加载XML配置的完整步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    spring配置文件详解--真的蛮详细

    Spring配置文件是一个或多个标准的XML文档,applicationContext.xml是Spring的默认配置文件,当容器启动时找不到指定的配置文档时,将会尝试加载这个默认的配置文件。 下面列举的是一份比较完整的配置文件模板,...

    DWR.xml配置文件说明书(含源码)

    DWR.xml配置文件说明书 1、 建立dwr.xml 配置文件 任何一个dwr.xml的文件都需要包含DWR DOCTYPE的声明行,格式如下: &lt;!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" ...

    spring-boot-camel-xml:一个快速入门,展示了如何将Spring Boot和camel与XML DSL以及Kubernetes或OpenShift一起使用

    本示例演示了如何通过Spring XML配置文件在Spring Boot中配置骆驼路线。 该应用程序利用Spring 批注通过类路径上的src / main / resources / spring / camel-context.xml文件加载Camel Context定义。 重要的 该...

    Spring_SpringMVC+MyBatis框架整合配置文件,解压即可使用

    这个是使用Spring+SpringMVC+MyBatis整合过程中的配置文件.考虑到很多朋友不知道在使用SSM时如何配置,以及配置复杂的情况下,我把这个解压即可使用的配置文件提供给大家.里面包括了config.properties(数据库配置参数)...

    基于Spring Boot的班级权限管理系统设计源码

    基于Spring Boot的班级权限管理系统设计源码,该项目包含117个文件,主要文件类型有97个java源文件,9个xml配置文件,以及3个gz压缩文件。此外,还包括2个properties配置文件,1个gitignore文件用于版本控制,1个jar...

    Spring MVC入门教程

    个人认为相当适合入门和知识巩固!! 一、前言 二、spring mvc 核心类与接口 三、spring mvc 核心流程图 四、spring mvc DispatcherServlet说明 ...二十、 本文中springMVC.xml配置文件是核心,这里给一个下载地址

    基于Spring Boot和Spring Cloud的微服务架构设计源码

    文件类型包括223个Java源代码文件、48个XML配置文件、36个YAML配置文件、35个PNG图片文件、7个YAML文件、6个SQL文件、5个Markdown文档、5个TXT文档、4个VM文件和3个LauncherService文件。该架构是一个商业级项目升级...

    Spring Boot中整合MyBatis

    关于SpringBoot中如何配置数据层MyBatis,以及多数据源的配置,多个数据层mapperInterface和xml文件路径的配置源码

    Spring系列之Spring常用注解总结.docx

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop、事物,这么做有两个缺点: 1、如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大;如果按需求分开.xml文件,那么.xml文件又会非常多。...

    基于Spring Boot和Vue 3的电子病历管理系统设计源码

    基于Spring Boot和Vue 3的电子病历管理系统设计源码,该项目包含278个文件,主要文件类型有88个xml配置文件,67个vue前端文件,以及47个java源文件。此外,还包括23个jpg图像文件,22个png图像文件,以及4个...

    SpringMVC框架架构介绍

    一、前言 二、spring mvc 核心类与接口 三、spring mvc 核心流程图 四、spring mvc DispatcherServlet说明 五、spring mvc 双亲上下文的说明 ...二十、 本文中springMVC.xml配置文件是核心,这里给一个下载地址

    Java之Spring Boot详解

    简单的来说 Spring Boot 这个项目整合目前很多的流行的第三方框架,并且做了一系列的默认配置,我们无需在进行手动配置,直接拿过来直接使用! 接下来我们比较一下Spring mvc和 Spring Boot的开发过程 Spring mvc (1)...

    Spring MVC 教程 快速入门 深入分析

    Spring MVC 教程 快速入门 深入分析 ...十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean 十八、spring mvc 多视图控制器 十九、 &lt;mvc:annotation-driven /&gt; 到底做了什么工作

    基于Spring Boot和多语言的sg-exam教学管理平台设计源码

    项目共包含1820个文件,其中Java源代码文件510个,TypeScript源代码文件477个,Vue组件文件316个,JavaScript源代码文件89个,SVG矢量图文件65个,PNG图片文件58个,XML配置文件50个,JPEG图片文件41个,LESS样式...

    使用Spring更好地处理Struts动作

    用,留给您的简单任务就只是在一个 XML 配置文件中把它们连接好。 依赖注入是一个强大的特性,但是 Spring 框架能够提供更多特性。Spring 支持可插拔的事务管理器,可以给您的事务处理提供更广泛的选择范围。它集成...

    基于SpringBoot,Spring Security,JWT,Vue & Element 的前后端分离权限管理系统设计源码

    此外,还包括87个svg图像文件,76个javascript文件,以及28个xml配置文件。该项目是一个基于SpringBoot,Spring Security,JWT,Vue & Element 的前后端分离权限管理系统设计源码,可能涉及用户界面设计、应用逻辑...

Global site tag (gtag.js) - Google Analytics