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 ...
随机推荐
- 鸟瞰Nodejs
一,基础. 1,Node的包管理器:npm; 安装node环境时会自动安装. 本地模式获取一个包:npm install [package_name] 此时包被安装到当前木的node_modules子 ...
- 用c#开发微信 (16) 微活动 2 刮刮卡
微信营销是一种新型的营销模式,由于微信更重视用户之间的互动,故而这种营销推广不不能盲目地套用微博营销的单纯大量广告推送方式.这种方式在微信营销中的效果非常差,会令用户反感,继而取消去企业或商家的微信公 ...
- VisualSvn server 权限配置
库上,配置 EveryOne 有读写权限. 下面的文件夹,再根据情况,取消 EveryOne 的读写权限,添加另一个用户组的读写权限. 它的规则是: 子目录权限覆盖父目录权限.
- C++ 面向对象编程
<C++ Primer 4th>读书笔记 面向对象编程基于三个基本概念:数据抽象.继承和动态绑定.在 C++ 中,用类进行数据抽象,用类派生从一个类继承另一个:派生类继承基类的成员.动态绑 ...
- spring常用jar包总结(转载)
spring.jar是包含有完整发布的单个jar 包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到 spring-m ...
- 【原】在windows下使用VirtualEnv
VirtualEnv可以方便的解决不同项目中对类库的依赖问题.这通常是通过以下方式实现的:首先将常用的类库安装在系统环境中:然后为每个项目安装独立的类库环境.这样子可以保证每个项目都运行在独立的类库环 ...
- paip.突破 网站 手机 验证码 的 破解 总结
paip.突破 网站 手机 验证码 的 破解 总结 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn ...
- document对象操作:浏览器页面文件
//找元素 1.根据id找 <div id="d1" cs="ceshi"><span>document对象</span>& ...
- Oracle 查看哪个表被锁定,并获取对应的sessionID
SELECT A.OWNER,A.OBJECT_NAME,B.XIDUSN,B.XIDSLOT,B.XIDSQN,B.SESSION_ID,B.ORACLE_USERNAME, B.OS_USER_N ...
- Category和Extension
转: http://blog.csdn.net/leikezhu1981/article/details/19091049 一.概述 类别是一种为现有的类添加新方法的方式. 利用Objective-C ...