对于spring配置文件的编写,我想,对于经历过庞大项目的人,都有那种恐惧的心理,太多的配置文件。不过,分模块都是大多数人能想到的方法,但是,怎么分模块,那就是仁者见仁,智者见智了。我的策略是使用import。
基本代码格式如下
 web.xml

<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans> <import resource="CTIContext.xml" />
<import resource="customerContext.xml" />
<import resource="customerServingContext.xml" />
<import resource="manageContext.xml" />
<import resource="routineContext.xml" />
<import resource="systemContext.xml" />
</beans>
  applicationContext.xml文件中使用import的方式导入有模块配置文件,以后若有新模块的加入,那就可以简单修改这个文件了,这样大大的简化了配置文件后期的复杂程度;
 
  Spring 的解析代码如下;
 /**
* Parse an "import" element and load the bean definitions from the given resource
* into the bean factory.
*/
protected void importBeanDefinitionResource(Element ele) {
// 获取resource 属性
String location = ele.getAttribute(RESOURCE_ATTRIBUTE);
// 如果不存在resource ,则不做任何处理
if (!StringUtils.hasText(location)) {
getReaderContext().error("Resource location must not be empty", ele);
return;
} // Resolve system properties: e.g. "${user.dir}"
// 解析系统属性.格式如 : ${user.dir}
location = environment.resolveRequiredPlaceholders(location); Set<Resource> actualResources = new LinkedHashSet<Resource>(4); // Discover whether the location is an absolute or relative URI
// 判断是绝对地址还是相对地址
boolean absoluteLocation = false;
try {
absoluteLocation = ResourcePatternUtils.isUrl(location)
|| ResourceUtils.toURI(location).isAbsolute();
}
catch (URISyntaxException ex) {
// cannot convert to an URI, considering the location relative
// unless it is the well-known Spring prefix "classpath*:"
} // Absolute or relative?
// 如果是绝对地址,则直接根据地址加载对应的配置文件
if (absoluteLocation) {
try {
int importCount = getReaderContext().getReader().loadBeanDefinitions(
location, actualResources);
if (logger.isDebugEnabled()) {
logger.debug("Imported " + importCount
+ " bean definitions from URL location [" + location + "]");
}
}
catch (BeanDefinitionStoreException ex) {
getReaderContext().error(
"Failed to import bean definitions from URL location ["
+ location + "]", ele, ex);
}
}
else {
// No URL -> considering resource location as relative to the current file.
// 如果是相对地址,则计算出绝对地址
try {
int importCount;
// Resource 的多个子类 ,如 VfsResource,FileSystemResource,ClassPathResource
// 而每个Resource的createRelative 方法都不太一样所以这里先使用子类的方法尝试解析,
Resource relativeResource = getReaderContext().getResource().createRelative(
location);
if (relativeResource.exists()) {
importCount = getReaderContext().getReader().loadBeanDefinitions(
relativeResource);
actualResources.add(relativeResource);
}
else {
// 如果解析不成功,则使用默认解析器ResourcePatternResolver 进行解析
String baseLocation = getReaderContext().getResource().getURL().toString();
importCount = getReaderContext().getReader().loadBeanDefinitions(
StringUtils.applyRelativePath(baseLocation, location),
actualResources);
}
if (logger.isDebugEnabled()) {
logger.debug("Imported " + importCount
+ " bean definitions from relative location [" + location
+ "]");
}
}
catch (IOException ex) {
getReaderContext().error("Failed to resolve current resource location",
ele, ex);
}
catch (BeanDefinitionStoreException ex) {
getReaderContext().error(
"Failed to import bean definitions from relative location ["
+ location + "]", ele, ex);
}
}
// 解析后进行监听器激活处理
Resource[] actResArray = actualResources.toArray(new Resource[actualResources.size()]);
getReaderContext().fireImportProcessed(location, actResArray, extractSource(ele));
}

4.2 spring-import 标签的解析;的更多相关文章

  1. 使用import简化spring的配置 spring import 标签的解析 使用import或加载spring配置时,报错误There is no ID/IDREF 多个Spring配置文件import resource路径配置

    spring-import 标签的解析.使用案例: 对于spring配置文件的编写,我想,对于经历过庞大项目的人,都有那种恐惧的心理,太多的配置文件.不过,分模块都是大多数人能想到的方法,但是,怎么分 ...

  2. Spring <import>标签配置

    使用情景:在Maven项目中,我们在Spring 配置文件中需要用到<import resource="">标签来引入其他配置文件,这里我要记下一些注意事项 情景1 & ...

  3. 【死磕 Spring】—– IOC 之解析Bean:解析 import 标签

    原文出自:http://cmsblogs.com 在博客[死磕Spring]----- IOC 之 注册 BeanDefinition中分析到,Spring 中有两种解析 Bean 的方式.如果根节点 ...

  4. Spring 系列教程之默认标签的解析

    Spring 系列教程之默认标签的解析 之前提到过 Spring 中的标签包括默认标签和自定义标签两种,而两种标签的用法以及解析方式存在着很大的不同,本章节重点带领读者详细分析默认标签的解析过程. 默 ...

  5. spring源码深度解析— IOC 之 默认标签解析(下)

    在spring源码深度解析— IOC 之 默认标签解析(上)中我们已经完成了从xml配置文件到BeanDefinition的转换,转换后的实例是GenericBeanDefinition的实例.本文主 ...

  6. spring源码学习之默认标签的解析(二)

    这个是接着上一篇来写,主要是这章内容比较多,还是分开来写吧! 一.AbstractBeanDefinition属性介绍 XML中的所有的属性都可以在GenericBeanDefinition中找到对应 ...

  7. Spring源码学习-容器BeanFactory(四) BeanDefinition的创建-自定义标签的解析.md

    写在前面 上文Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签对Spring默认标签的解析做了详解,在xml元素的解析中,Spri ...

  8. 4.4 spring-自定义标签的解析

    1.0 自定义标签的解析. 在之前的章节中,我们完成了对spring 默认标签的加载过程.那么现在我们将开始新的里程, spring 自定义标签的解析; 代码如下: /** * Parse the e ...

  9. 《spring源码解读》 - IoC 之解析 import 标签

    在上一文中我们分析了注册 BeanDefinition 的过程,在其中我们了解到在解析跟节点和子节点时分两种情况,对于默认名称空间的标签我们通过 DefaultBeanDefinitionDocume ...

  10. 【Spring源码深度解析学习系列】默认标签解析(三)

    Spring的标签包括默认标签和自定义标签两种 默认标签的解析方法: ###DefaultBeanDefinitionDocumentReader.java### private void parse ...

随机推荐

  1. Ajax返回html和json格式数据

    Ajax可以返回text和xml格式 可以用Ajax返回大段的html文本和json格式的字符串,然后用eval()方法 转化为json对象 php中的json编码:json_encode(); ph ...

  2. Ehcache(2.9.x) - API Developer Guide, Basic Caching

    Creating a CacheManager All usages of the Ehcache API start with the creation of a CacheManager. The ...

  3. Linux 命令 - umask: 显示或设置文件模式掩码值

    umask 命令控制着创建文件时指定给文件的默认权限.它使用八进制表示法从文件模式属性中删除一个位掩码. 参见下面的例子: [huey@huey-K42JE cmdline]$ rm -f foo.t ...

  4. sql中nvarchar(max)长度测试

    nvarchar(max)长度测试:在使用convert强制类型转化之后 文本长度可以突破8000的上限.并且nvarchar(max)的最大长度可达到2^31以下为验证SQL: Declare @A ...

  5. JAVA之经典Student问题1

    通过“三目运算符”求最大值与最小值. class student { //定义学生编号 private String stu; //学生姓名 private String name; //学生书信成绩 ...

  6. Oracle 硬解析查询

    -- 硬解析的 parse count (hard) select * from v$sysstat where name like '%parse%'; select a.value,b.name ...

  7. 改变UITextField placeHolder 字体 颜色

    [_textSearchField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; ...

  8. 快速搭建MongoDB分布式集群

    目录Outline 1. prerequisites 2. steps to follow3. configuring the cluster4. a little test to see 1. Pr ...

  9. bzoj3405:[Usaco2009 Open]Grazing2 移动牛棚

    思路:首先因为要让距离尽量大,所以奶牛1一定在1号牛棚,奶牛n一定在s号牛棚,然后考虑dp. 因为总距离为s-1,然后要使长度为d的段数尽量多,那么剩下的一定就是d+1的段数,也就是s-(n-1)*d ...

  10. Redirect and POST in ASP.NET

    http://www.codeproject.com/Articles/37539/Redirect-and-POST-in-ASP-NET