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. Android 8 wifi blakclist

    在连接wifi的时候,认证或者关联失败,有时会加入黑名单中.记录wpa_supplicant中blacklist的原理. 分析可以看到,如果是机器自己断开,是不会把AP加入黑名单的,只有AP侧出了问题 ...

  2. 简单入门dos程序

    --1.关机程序 注意:文件保存为.bat echo 晚安了,宝贝! @echo off shutdown -s -t exist --2.快捷/批量启动程序 title "程序系统启动&q ...

  3. 表单提交之List集合

    一.表单数据 <div class="panel panel-default"> <div class="panel-heading"> ...

  4. 指定webapi 返回 json 格式 ; GlobalConfiguration.Configuration.Formatters.Clear()

    因为 Internet Explorer 和 Firefox 发送了不同的 Accept 头,所以 web API 在响应里就发送了不同的内容类型.   解决方法,在 Global.asax的 App ...

  5. UITableView:可展开的 UITableView

    针对 TableView,有些时候需要在点击 cell 时,展开这行 cell,显现出更多的选项或者全部内容等. 比较容易想到的处理方案就是利用 section,在未选择之前,每一行都是一个 sect ...

  6. (转)GCT之逻辑经验总结(拿来主义)

    GCT逻辑考试,并非考核逻辑专业知识,而是考核考生的日常逻辑思维能力.应该说日常逻辑思维能力是人在成长过程中及在社会活动中形成的,因此,只要运用好这种能力,就能取得逻辑考试的好成绩.因此可以认为:GC ...

  7. Android学习之——ListView

    背景知识 ListView在Android应用中使用非常广泛,手机上必备的微博.网易新闻等,都使用了ListView.

  8. RMAN:简单的duplicate创建新数据库

    duplicate to "test" backup location '/home/oracle/11.2.0.4/assistants/dbca/templates/'; du ...

  9. Oracle-10.2.0.1,打补丁10.2.0.5:在 debian 版本4【不含4】以上,及 ubuntu 7.04【不含7.04】以上都可以安装!

    如题. todo 特殊的:ubuntu 16.04 LTS 版本 无法安装成功,原因待查找!!! 最近测试练习安装linux x64上的 oracle10.2.0.5, 都要吐了.

  10. android LinearLayout添加分隔线

    方法一: 可以放置一个ImageView组件,然后将其设为分隔线的颜色或图形. 分隔线View的定义代码如下: [html] view plaincopy   <ImageView androi ...