Spring3系列4-多个配置文件的整合
Spring3系列4-多个配置文件的整合
在大型的Spring3项目中,所有的Bean配置在一个配置文件中不易管理,也不利于团队开发,通常在开发过程中,我们会按照功能模块的不同,或者开发人员的不同,将配置文件分成多个,这样有利于模块的划分。本文将讲述怎样整合多个配置文件,由于Spring3允许使用xml配置和JavaConfig特性两种方式配置,本文也将分别举例。
一、 加载多个xml文件配置
例如,项目中有多个xml配置文件:
- Spring-Common.xml位于common文件夹下
- Spring-Connection.xml位于connection文件夹下
- 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的配置:
- CustomerConfig.java
- 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-多个配置文件的整合的更多相关文章
- Spring3系列13-Controller和@RequestMapping
Spring3系列13-Controller和@RequestMapping Controller返回值,String或者ModelAndView @RequestMapping关联url @Requ ...
- Spring3系列12- Spring AOP AspectJ
Spring3系列12- Spring AOP AspectJ 本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代 ...
- Spring3系列11- Spring AOP——自动创建Proxy
Spring3系列11- Spring AOP——自动创建Proxy 在<Spring3系列9- Spring AOP——Advice>和<Spring3系列10- Spring A ...
- Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法
Spring3系列10- Spring AOP——Pointcut,Advisor 上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都 ...
- Spring3系列9- Spring AOP——Advice
Spring3系列9- Spring AOP——Advice Spring AOP即Aspect-oriented programming,面向切面编程,是作为面向对象编程的一种补充,专门用于处理系统 ...
- Spring3系列8- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- Spring3系列7- 自动扫描组件或Bean
Spring3系列7- 自动扫描组件或Bean 一. Spring Auto Scanning Components —— 自动扫描组件 1. Declares Component ...
- Spring3系列5-Bean的基本用法
Spring3系列5-Bean的基本用法 本篇讲述了Bean的基本配置方法,以及Spring中怎样运用Bean. 主要内容如下: 一. Spring中Bean的相互引用 二. Sp ...
- Spring3系列3 -- JavaConfig
Spring3系列3-JavaConfig-1 从Spring3开始,加入了JavaConfig特性,JavaConfig特性允许开发者不必在Spring的xml配置文件中定义bean,可以在Java ...
随机推荐
- 工资低的.Net程序员,活该你工资低
这两天博客园上关于“.Net工资低”的讨论挺多的,让我不禁想起一句话“拉不出屎来怪地球没引力”. 那些抱怨“做.Net工作三年了月薪才6千,我的同学做Java现在都一万二”的哥们,你问问自己“我会什么 ...
- 【转载】php中iconv函数使用方法
原文:http://www.phpweblog.net/star65225692/archive/2011/03/23/7524.html 在选择用什么工具开发,唯一的指导标准就是:用最少的人 ...
- 《The Book of CSS3》学习笔记
一.浏览器前缀 E{ -moz-name : value; /* Firefox */ -ms-name : value; /* IE */ -o-name : value; /* Opera */ ...
- 让Sqlite脱离VC++ Runtime独立运行
前段时间在开发OrayTalk(傲瑞通)的聊天记录模块时用到了Sqlite,这是我第一次接触和使用Sqlite,总体感觉还是非常不错的.这里把我使用Sqlite的经验跟大家分享一下. 一.关于Sqli ...
- [ACM_模拟] UVA 10881 Piotr's Ants[蚂蚁移动 数组映射 排序技巧]
"One thing is for certain: there is no stopping them;the ants will soon be here. And I, for one ...
- [BTS] The value "" for the property InboundId is invalid
Microsoft.ServiceModel.Channels.Common.MetadataException: Retrieval of Operation Metadata has failed ...
- iOS YSDropdownMagnify 下拉放大,向上导航显示
要实现的效果如上.在实际开发中,我们会使用到三种方式来实现. 通过隐藏导航栏,自定义导航View 改变原生导航栏背景透明 原生导航栏通过添加背景图片改变 个人是比较喜欢第二种. github下载地址: ...
- atittit.表单验证的实现方式以及原理本质以及选型以及自定义兼容easyui dsl规则的表单验证
atittit.表单验证的实现方式以及原理本质以及选型以及自定义兼容easyui dsl规则的表单验证 1. 需求,表单验证需要弹框式,但目前easyui ms绑定死了tooltip式样 1 2. 表 ...
- iOS开发之静态库(三)—— 图片、界面xib等资源文件封装到.a静态库
编译环境:Macbook Air + OS X 10.9.2 + XCode5.1 + iPhone5s(iOS7.0.3) 一.首先将资源文件打包成bundle 新建工程:File -> Ne ...
- iOS-程序发布-32位和64位系统的兼容
在苹果推出iPhone5S时,64位的应用就走到了眼前.当时就看见苹果官方资料宣布iOS7.x的SDK支持了64位的应用,而且内置的应用都已经是64位. 我记得自己刚刚接触电脑时还有16位的系统,指针 ...