XML-schema

2020-12-31  本文已影响0人  16325

首先来看一下项目中spring的一个配置文件

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/aop  
        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"
    default-lazy-init="true">

XML Schema命名空间作用:

1、避免命名冲突,像Java中的package一样
2、将不同作用的标签分门别类(像Spring中的tx命名空间针对事务类的标签,context命名空间针对组件的标签)

定义说明

注意这里特殊说明一下。xml引用了xsi的这个命名空间,后边才可以使用xsi:schemaLocation

不使用schemaLocation,直接再namespace上绑定xsd

如图:

<beans xmlns="http://www.springframework.org/schema/beans/spring-beans.xsd"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  

注意上边并没有指定xsd的版本,那么就回去对应的url获取,最新的版本。

schemaLocation

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

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

XSD 验证

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

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

spring.handlers:

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

spring.schemas:

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

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


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

如何跳过Spring的XML校验

GenericXmlApplicationContext context = new GenericXmlApplicationContext();  
context.setValidating(false);  

如何写一个自己的spring xml namespace扩展

可以参考Spring的文档,实际上是相当简单的。只要实现自己的NamespaceHandler,再配置一下spring.handlers和spring.schemas就可以了。
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/extensible-xml.html

添加自定义的xsd

如果我们需要添加自定义的xsd,并且需要本地引用,那么需要这样加载:


image.png
上一篇 下一篇

猜你喜欢

热点阅读