XML的一些概念

首先来看下xml的一些概念:

xml的schema里有namespace,可以给它起个别名。比如常见的spring的namespace:

  1.  
    xmlns:mvc="http://www.springframework.org/schema/mvc"
  2.  
    xmlns:context="http://www.springframework.org/schema/context"

通常情况下,namespace对应的URI是一个存放XSD的地址,尽管规范没有这么要求。如果没有提供schemaLocation,那么Spring的XML解析器会从namespace的URI里加载XSD文件。我们可以把配置文件改成这个样子,也是可以正常工作的:

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans/spring-beans.xsd"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

schemaLocation提供了一个xml namespace到对应的XSD文件的一个映射,所以我们可以看到,在xsi:schemaLocation后面配置的字符串都是成对的,前面的是namespace的URI,后面是xsd文件的URI。比如:

  1.  
    xsi:schemaLocation="http://www.springframework.org/schema/beans
  2.  
    http://www.springframework.org/schema/beans/spring-beans.xsd
  3.  
    http://www.springframework.org/schema/security
  4.  
    http://www.springframework.org/schema/security/spring-security.xsd"

Spring是如何校验XML的

Spring默认在启动时是要加载XSD文件来验证xml文件的,所以如果有的时候断网了,或者一些开源软件切换域名,那么就很容易碰到应用启动不了。

为了防止这种情况,Spring提供了一种机制,默认从本地加载XSD文件。打开spring-context-3.2.0.RELEASE.jar,可以看到里面有两个特别的文件:

spring.handlers

  1.  
    http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler
  2.  
    http\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler
  3.  
    http\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler
  4.  
    http\://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandler
  5.  
    http\://www.springframework.org/schema/cache=org.springframework.cache.config.CacheNamespaceHandler

spring.schemas

  1.  
    http\://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd
  2.  
    http\://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd
  3.  
    http\://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsd
  4.  
    http\://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/spring-context-3.2.xsd
  5.  
    http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-3.2.xsd
  6.  
    ...

再打开jar包里的org/springframework/context/config/ 目录,可以看到下面有

spring-context-2.5.xsd
spring-context-3.0.xsd
spring-context-3.1.xsd
spring-context-3.2.xsd

很明显,可以想到Spring是把XSD文件放到本地了,再在spring.schemas里做了一个映射,优先从本地里加载XSD文件。

并且Spring很贴心,把旧版本的XSD文件也全放了。这样可以防止升级了Spring版本,而配置文件里用的还是旧版本的XSD文件,然后断网了,应用启动不了。

我们还可以看到,在没有配置版本号时,用的就是当前版本的XSD文件:

http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-3.2.xsd
 

如何跳过Spring的XML校验?

可以用这样的方式来跳过校验:

  1.  
    GenericXmlApplicationContext context = new GenericXmlApplicationContext();
  2.  
    context.setValidating(false);
</pre><pre code_snippet_id="264875" snippet_file_name="blog_20140330_11_9075882" name="code" class="html" style="border: 1px solid rgb(255, 255, 204); overflow: auto;">
 

这样可以解决在断网情况下加载xsd文件连接超时的问题。
schema_reference.4:无法读取方案文档 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd,原因是1)无法找到文档 2)无法读取文档 3)文档的根元素不是<xsd:schema>.
caused by java.net.ConnectionException:Connection timed out:connect.
...
原因就是在spring的jar包中META-INF/spring.schemas中没有加上
http\://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd
http\://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd
http\://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd
http\://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-3.0.xsd
没有加上这个,则xsd加载时就会去网上找,在无网情况下就会报错。
另外,使用eclipse的导出为可运行jar包时,多个spring jar包同时有,spring.handlers、spring.schemas、spring.tooling三个文
件,但是打成jar包时在META-INF/下只能留一份,这时需要将多个文件中的内容合并在一个文件中。

转自https://blog.csdn.net/dingqinghu/article/details/46758671

spring配置文件中xsd引用问题的更多相关文章

  1. 转:spring配置文件中xsd引用问题

    来自:http://blog.csdn.net/dingqinghu/article/details/46758671

  2. Spring配置文件中未引入dubbo命名空间头部配置而引起的错误案例

    问题描述: Spring配置文件中未引入dubbo命名空间的头部配置而引起项目启动时报出如下错误信息: org.springframework.beans.factory.xml.XmlBeanDef ...

  3. Spring依赖注入的方式、类型、Bean的作用域、自动注入、在Spring配置文件中引入属性文件

    1.Spring依赖注入的方式 通过set方法完成依赖注入 通过构造方法完成依赖注入 2.依赖注入的类型 基本数据类型和字符串 使用value属性 如果是指向另一个对象的引入 使用ref属性 User ...

  4. Spring配置文件的xsd知识点

    今天在Spring配置文件中配置如下事务属性时,提示<tx is not bound(不受约束的),估计是配置文件的xsd没配置好. <!-- 2.配置事务属性 --> <tx ...

  5. 通过Spring配置文件中bean中的property赋值

    基本数据类型赋值-通过spring配置文件中bean中的property 扩展-以此方式可以通过配置为连接数据的属性赋值 1.如果是基本数据类型,可以通过setter方法为对象中的属性设置初始值,应用 ...

  6. Spring配置文件中使用ref local与ref bean的区别

    Spring配置文件中使用ref local与ref bean的区别.在ApplicationResources.properties文件中,使用<ref bean>与<ref lo ...

  7. 在spring配置文件中引入外部properties配置文件 context:property-placeholder

    在spring的配置文件中,有时我们需要注入很多属性值,这些属性全都写在spring的配置文件中的话,后期管理起来会非常麻烦.所以我们可以把某一类的属性抽取到一个外部配置文件中,使用时通用spring ...

  8. Spring 源码(4)在Spring配置文件中自定义标签如何实现?

    Spring 配置文件自定义标签的前置条件 在上一篇文章https://www.cnblogs.com/redwinter/p/16165274.html Spring BeanFactory的创建过 ...

  9. Spring配置文件中的parent与abstract

    在看项目的Spring配置文件时,发现消息队列的配置采用了继承方式配置Bean,在这梳理总结一下. 其实在基于spring框架开发的项目中,如果有多个bean都是一个类的实例,如配置多个数据源时,大部 ...

随机推荐

  1. (笔记)Linux下的CGI和BOA使用期间遇到的问题汇总

    前段时间在做C/S模式下的视频监控,这段时间是B/S模式下的.期间遇到了不少问题,有些问题一卡就是几天,有些问题的解决办法在办法在网上也不是很好找,所以还有些问题虽然得到了临时解决,但是其原理现在我本 ...

  2. VIM下的普通模式的相关知识

    什么为一次操作? 从进行插入模式开始,直到返回普通模式为止,在此期间的任何修改都视为一次操作:   使用 u 可以撤销最新的修改: 所以呢,控制好在插入模式的操作就可以控制好撤销命令的粒度: 另外,最 ...

  3. 第三百四十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—爬虫和反爬的对抗过程以及策略—scrapy架构源码分析图

    第三百四十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—爬虫和反爬的对抗过程以及策略—scrapy架构源码分析图 1.基本概念 2.反爬虫的目的 3.爬虫和反爬的对抗过程以及策略 scra ...

  4. 关于对最新HTML总结PPT讲稿的分享

    如果大家还记得HTML,那么2009年的时候可能当时还是HTML1.0时代,而国际化的标准才刚刚开始,对于TABLE表格的使用,还有就是一些常用的标签都是及为简单的,因为当时的代码都是接近于短码,所以 ...

  5. Cisco配置单臂路由及静态路由

    实验环境: 如图下图所示,PC0.PC1.PC2.PC3分别属于不同的VLAN,通过配置单臂路由及静态路由,实现不同VLAN之间的PC能相互访问. 操作步骤: 1. 思科 2960交换机SW1配置信息 ...

  6. CentOS7环境RabbitMQ集群配置管理

    CentOS7系统内核版本:3.10.0-514.26.2.el7.x86_64 一.对应主机host地址(三台主机host文件要保持一致) 10.100.2.10 v01-app-rabbitmq0 ...

  7. R语言数据框小技巧

    当我们想要把数据框的行或者列按照指定的顺序排列时,可以通过行名称或者列名称快速排列 data <- data.frame(matrix(1:9, ncol=3)) rownames(data) ...

  8. c#包含类文件到csprj中

    public ActionResult Index() { XDocument doc = XDocument.Load(@"G:\users\kim.gao\documents\visua ...

  9. 多个IoC容器适配器设计及性能测试(Castle.Windsor Autofac Spring.Core)

    [转]多个IoC容器适配器设计及性能测试和容器选择 1. 采用的IoC容器和版本 Autofac.2.6.3.862 Castle.Windsor.3.1.0 Spring.Core.2.0.0 2. ...

  10. npm install mongoose错误解决

    今天安装mongoose一直报错,上图 具体的错误记录: info it worked if it ends with ok verbose cli [ 'C:\\Program Files\\nod ...