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 ...
随机推荐
- linux-6的yum软件仓库
yum命令 命令 作用 yum repolist all 列出所有仓库 yum list all 列出仓库中的所有软件包 yum info 软件包名称 查看软件包信息 yum install ...
- java的四种引用,强弱软虚
1.利用软引用和弱引用解决OOM问题:用一个HashMap来保存图片的路径和相应图片对象关联的软引用之间的映射关系,在内存不足时,JVM会自动回收这些缓存图片对象所占用的空间,从而有效地避免了OOM的 ...
- 基于python的堡垒机
一 堡垒机的架构 堡垒机的核心架构通常如下图所示: 二.堡垒机的一般执行流程 管理员为用户在服务器上创建账号(将公钥放置服务器,或者使用用户名密码) 用户登陆堡垒机,输入堡垒机用户名密码,显示当前用户 ...
- C#函数式编程之惰性求值
惰性求值 在开始介绍今天要讲的知识之前,我们想要理解严格求值策略和非严格求值策略之间的区别,这样我们才能够深有体会的明白为什么需要利用这个技术.首先需要说明的是C#语言小部分采用了非严格求值策略,大部 ...
- NABCD需求分析
1. N 需求 如今的社会,每个公司每个人都有大量的信息需要处理.保管和查询,这就难免会有些信息在个人电脑中保存的位置被遗忘,需要一款简单并且实用的搜索引擎来搜索个人 所需求的信息. 2. A ...
- 自己动手做Web框架—MVC+Front Controller
在我前面一篇博文<逃脱Asp.Net MVC框架的枷锁,使用Razor视图引擎>发表之后,很多人关心,脱离了之后怎么办?那么这可以说是它的续篇了. 同时,这也是eLiteWeb开源软件的一 ...
- 为什么不能把委托(delegate)放在一个接口(interface)当中?
stackoverflow上有人问,为什么不能把委托放在一个接口当中? 投票最多的第一个答案第一句话说,“A Delegate is just another type, so you don't g ...
- lucene join解决父子关系索引
http://www.cnblogs.com/LBSer/p/4417074.html 1 背景 以商家(Poi)维度来展示各种服务(比如团购(deal).直连)正变得越来越流行(图1a), 比如目前 ...
- paip.php 5.0 5.3 5.4 5.5 -6.0的新特性总结与比较
paip.php 5.0 5.3 5.4 5.5 -6.0的新特性总结与比较 PHP5的新特性 2 · 对象的参照过渡是默认的(default) 3 · 引入访问属性的限制 3 · 引入访问方法的限 ...
- paip.消除 Java 的冗长try/catch/finally
paip.消除 Java 的冗长try/catch/finally 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blo ...