Spring3系列4-多个配置文件的整合

  在大型的Spring3项目中,所有的Bean配置在一个配置文件中不易管理,也不利于团队开发,通常在开发过程中,我们会按照功能模块的不同,或者开发人员的不同,将配置文件分成多个,这样有利于模块的划分。本文将讲述怎样整合多个配置文件,由于Spring3允许使用xml配置和JavaConfig特性两种方式配置,本文也将分别举例。

一、      加载多个xml文件配置

  例如,项目中有多个xml配置文件:

  1. Spring-Common.xml位于common文件夹下
  2. Spring-Connection.xml位于connection文件夹下
  3. Spring-ModuleA.xml位于moduleA文件夹下

  你可以在代码中加载以上3个xml配置文件

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Common.xml","Spring-Connection.xml","Spring-ModuleA.xml"});

  但是这种方法不易组织并且不好维护,最好的方法是在一个单独的xml的配置文件中组织其他所有的xml配置文件。例如,可以创建一个Spring-All-Module.xml文件,然后将其他的xml配置文件导入到Spring-All-Module.xml中,就像下边这样,

Spring-All-Module.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <import resource="common/Spring-Common.xml"/>
<import resource="connection/Spring-Connection.xml"/>
<import resource="moduleA/Spring-ModuleA.xml"/>
</beans>

现在,你可以在代码中加载一个单独的xml配置文件,如下:

ApplicationContext context =    new ClassPathXmlApplicationContext(Spring-All-Module.xml);

二、      加载多个JavaConfig特性配置

  JavaConfig特性配置SpringBean见前文《Spring3系列3-JavaConfig》

  假设有两个JavaConfig的配置:

  1. CustomerConfig.java
  2. SchedulerConfig.java

  你需要管理多个JavaConfig配置的情况下,可以单独创建一个AppConfig.java,然后将其他的配置导入到AppConfig.java中,如下:

AppConfig.java

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; @Configuration
@Import({ CustomerConfig.class, SchedulerConfig.class })
public class AppConfig { }

这样,加载时,只需要加载AppConfig.java即可

ApplicationContext context =
  new AnnotationConfigApplicationContext(AppConfig.class);

Spring3系列4-多个配置文件的整合的更多相关文章

  1. Spring3系列13-Controller和@RequestMapping

    Spring3系列13-Controller和@RequestMapping Controller返回值,String或者ModelAndView @RequestMapping关联url @Requ ...

  2. Spring3系列12- Spring AOP AspectJ

    Spring3系列12- Spring AOP AspectJ 本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代 ...

  3. Spring3系列11- Spring AOP——自动创建Proxy

    Spring3系列11- Spring AOP——自动创建Proxy 在<Spring3系列9- Spring AOP——Advice>和<Spring3系列10- Spring A ...

  4. Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法

    Spring3系列10- Spring AOP——Pointcut,Advisor 上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都 ...

  5. Spring3系列9- Spring AOP——Advice

    Spring3系列9- Spring AOP——Advice Spring AOP即Aspect-oriented programming,面向切面编程,是作为面向对象编程的一种补充,专门用于处理系统 ...

  6. Spring3系列8- Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

  7. Spring3系列7- 自动扫描组件或Bean

    Spring3系列7- 自动扫描组件或Bean 一.      Spring Auto Scanning Components —— 自动扫描组件 1.      Declares Component ...

  8. Spring3系列5-Bean的基本用法

    Spring3系列5-Bean的基本用法 本篇讲述了Bean的基本配置方法,以及Spring中怎样运用Bean. 主要内容如下: 一.      Spring中Bean的相互引用 二.      Sp ...

  9. Spring3系列3 -- JavaConfig

    Spring3系列3-JavaConfig-1 从Spring3开始,加入了JavaConfig特性,JavaConfig特性允许开发者不必在Spring的xml配置文件中定义bean,可以在Java ...

随机推荐

  1. Attribute和Property

    有时很容易对Attribute和Property混淆,因为中文翻译都是“属性”来解释的.其实这两个表达的不是一个层面的东西. Property属于面向对象理论范畴,在使用面向对象思想编程的时候,常常需 ...

  2. C#设计模式(19)——状态者模式(State Pattern)

    一.引言 在上一篇文章介绍到可以使用状态者模式和观察者模式来解决中介者模式存在的问题,在本文中将首先通过一个银行账户的例子来解释状态者模式,通过这个例子使大家可以对状态者模式有一个清楚的认识,接着,再 ...

  3. ".NET Core Open Source Update"阅读笔记

    原文链接:.NET Core Open Source Update [Immo Landwerth发布于2015年1月28日] corefx在github上的forks已经超过1000. 从2014年 ...

  4. C++11 并发指南六(atomic 类型详解一 atomic_flag 介绍)

    C++11 并发指南已经写了 5 章,前五章重点介绍了多线程编程方面的内容,但大部分内容只涉及多线程.互斥量.条件变量和异步编程相关的 API,C++11 程序员完全可以不必知道这些 API 在底层是 ...

  5. 你不得不知道的HTML5的新型标签

    <article>标签定义外部的内容.比如来自一个外部的新闻提供者的一篇新的文章,或者来自 blog 的文本,或者是来自论坛的文本.亦或是来自其他外部源内容. <aside>标 ...

  6. 创建一个简单的HTTP服务(自动查找未使用的端口)

    var t = new Thread(new ThreadStart(() => { HttpListener listener = new HttpListener(); var prefix ...

  7. [游戏学习24] MFC 各种绘图 字体学习

    >_<:这里包含字体设置及各种绘图,只要稍微看一下代码就能理解,这里不多介绍 >_<:Hello.h #include<afxwin.h> class CMyApp ...

  8. 使用SharePoint CSOM 编写高效的程序

    上一篇文章中简单的介绍了使用CSOM进行编程.今天主要讲一下CSOM使用中一些小技巧,可以让你的程序运行的更快. 单独加载某些属性 在上文中的例子,需要返回Web对象信息的时候,我们使用了如下的代码: ...

  9. iis6.0报以下的错。。

    Could not load file or assembly 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec8 ...

  10. atitit.java给属性赋值方法总结and BeanUtils 1.6.1 .copyProperty的bug

    atitit.java给属性赋值方法总结and BeanUtils 1.6.1 .copyProperty的bug 1. core.setProperty(o, "materialId&qu ...