`
mzhj
  • 浏览: 223864 次
  • 性别: 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;

分享到:
评论

相关推荐

    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的默认配置文件,当容器启动时找不到指定的配置文档时,将会尝试加载这个默认的配置文件。 下面列举的是一份比较完整的配置文件模板,...

    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定义。 重要的 该...

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

    spring beanName 从配置文件中读取的bean的名称 “scope参数允许你配置creator的生命周期,共有以下几个选项:application,session,request,page.这些参数对于用过jsp或servlet的开发人员并不陌生. 3.1 Uing static ...

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

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

    Spring MVC入门教程

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

    Spring Boot中整合MyBatis

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

    Spring-Reference_zh_CN(Spring中文参考手册)

    处理多个持久化单元 12.6.2. JpaTemplate 和 JpaDaoSupport 12.6.3. 基于原生的JPA实现DAO 12.6.4. 异常转化 12.6.5. 事务管理 12.6.6. JpaDialect III. Web 13. Web框架 13.1. 介绍 13.1.1. 与其他web框架的集成 ...

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

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

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    SpringMVC框架架构介绍

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

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

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

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

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

    基于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和多语言的sg-exam教学管理平台设计源码

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

    spring.net中文手册在线版

    4.15.从其它文件中导入对象定义 4.16.服务定位器访问 第五章. IObjectWrapper接口和类型转换 5.1.简介 5.2.使用IObjectWrapper接口管理对象 5.2.1.读、写普通及嵌套的属性 5.2.2.其它功能 5.3.类型转换 5.3.1.转换...

    基于java的企业级应用开发:Bean的配置.ppt

    Bean的配置 XML配置文件的根元素是,中包含了多个子元素,每一个子元素定义了一个Bean,并描述了该Bean如何被装配到Spring容器中。关于元素的常用属性如下表所示: 小提示:元素的更多属性,详见教材表2-1所示。 ...

Global site tag (gtag.js) - Google Analytics