spring命名空间不需要版本号
为什么dubbo启动没有问题?
这篇blog源于一个疑问:
我们公司使了阿里的dubbo,但是阿里的开源网站http://code.alibabatech.com,挂掉有好几个月了,为什么我们的应用启动没有问题?
我们的应用的Spring配置文件里有类似的配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
我们都知道Spring在启动时是要检验XML文件的。或者为什么在Eclipse里xml没有错误提示?
比如这样的一个Spring配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <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.xsd">
- </beans>
我们也可以在后面加上版本号:
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
有这个版本号和没有有什么区别呢?
XML的一些概念
首先来看下xml的一些概念:
xml的schema里有namespace,可以给它起个别名。比如常见的spring的namespace:
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
通常情况下,namespace对应的URI是一个存放XSD的地址,尽管规范没有这么要求。如果没有提供schemaLocation,那么Spring的XML解析器会从namespace的URI里加载XSD文件。我们可以把配置文件改成这个样子,也是可以正常工作的:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans/spring-beans.xsd"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
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"
Spring是如何校验XML的
Spring默认在启动时是要加载XSD文件来验证xml文件的,所以如果有的时候断网了,或者一些开源软件切换域名,那么就很容易碰到应用启动不了。我记得当时Oracle收购Sun公司时,遇到过这个情况。
为了防止这种情况,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/ 目录,可以看到下面有
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
同样,我们打开dubbo的jar包,可以在它的spring.schemas文件里看到有这样的配置:
- http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd
所以,Spring在加载dubbo时,会从dubbo的jar里加载dubbo.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加载不成功的一个思路
http://hellojava.info/?p=135
齐全的Spring的namespace的列表
http://stackoverflow.com/questions/11174286/spring-xml-namespaces-how-do-i-find-what-are-the-implementing-classes-behind-t
Spring core
aop-AopNamespaceHandlerc-SimpleConstructorNamespaceHandlercache-CacheNamespaceHandlercontext-ContextNamespaceHandlerjdbc-JdbcNamespaceHandlerjee-JeeNamespaceHandlerjms-JmsNamespaceHandlerlang-LangNamespaceHandlermvc-MvcNamespaceHandleroxm-OxmNamespaceHandlerp-SimplePropertyNamespaceHandlertask-TaskNamespaceHandlertx-TxNamespaceHandlerutil-UtilNamespaceHandler
Spring Security
security-SecurityNamespaceHandleroauth-OAuthSecurityNamespaceHandler
Spring integration
int-IntegrationNamespaceHandleramqp-AmqpNamespaceHandlerevent-EventNamespaceHandlerfeed-FeedNamespaceHandlerfile-FileNamespaceHandlerftp-FtpNamespaceHandlergemfire-GemfireIntegrationNamespaceHandlergroovy-GroovyNamespaceHandlerhttp-HttpNamespaceHandlerip-IpNamespaceHandlerjdbc-JdbcNamespaceHandlerjms-JmsNamespaceHandlerjmx-JmxNamespaceHandlermail-MailNamespaceHandlerredis-RedisNamespaceHandlerrmi-RmiNamespaceHandlerscript-ScriptNamespaceHandlersecurity-IntegrationSecurityNamespaceHandlersftp-SftpNamespaceHandlerstream-StreamNamespaceHandlertwitter-TwitterNamespaceHandlerws-WsNamespaceHandlerxml-IntegrationXmlNamespaceHandlerxmpp-XmppNamespaceHandler
总结:
为什么不要在Spring的配置里,配置上XSD的版本号?
因为如果没有配置版本号,取的就是当前jar里的XSD文件,减少了各种风险。
而且这样约定大于配置的方式很优雅。
参考:
http://stackoverflow.com/questions/10768873/spring-di-applicationcontext-xml-how-exactly-is-xsischemalocation-used
http://stackoverflow.com/questions/11174286/spring-xml-namespaces-how-do-i-find-what-are-the-implementing-classes-behind-t
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/extensible-xml.html
转:http://blog.csdn.net/hengyunabc/article/details/22295749
spring命名空间不需要版本号的更多相关文章
- Spring入门(3)-Spring命名空间与Bean作用域
Spring入门(3)-Spring命名空间与Bean作用域 这篇文章主要介绍Spring的命名空间和Bean作用域 0. 目录 Spring命名空间 Bean作用域 1. Spring命名空间 在前 ...
- spring命名空间
作为一名入门级菜鸟的我,在刚开始学spring框架的时候,对于命名空间不是很理解,只是在跟着老师敲代码.随着后来学习的深入,慢慢了解到命名空间的意义.如果你没有引入相应的命名空间,就不能引用相应的标签 ...
- Spring命名空间引入方法
spring 整合了各种工具,并且spring提供了对各种工具的xml scheme 的配置方式,简化了开发. 但是对于各种工具的xml命名空间的引入,我一直很郁闷,不知道应该怎样引入,今天经过摸索发 ...
- 覆盖io.spring.platform管理的版本号
使用io.spring.platform时,它会管理各类经过集成测试的依赖版本号.想要覆盖其中某个依赖的版本号个: https://www.cnblogs.com/ld-mars/p/11818252 ...
- 如何优雅的设计 Spring Boot API 接口版本号
原文:https://blog.mariojd.cn/how-to-design-spring-boot-api-version-number-elegantly.html 一般来说,系统上线以后,需 ...
- spring 命名空间
命名空间太多了,有必要学习了解一下 xmlns是XML Namespaces的缩写 使用语法: xmlns:namespace-prefix="namespaceURI" xsi全 ...
- Spring Cloud Alibaba 2021.0.1.0 发布:版本号再也不迷糊了
大家好,DD又来了! 3月9日,Spring官方博客发文:Spring Cloud Alibaba 2021.0.1.0发布了. 前段时间DD还在微信群里看到小伙伴吐槽Spring Cloud Ali ...
- Spring相框
1.什么是Spring相框?Spring有哪些主要模块框架? Spring框架是一个为Java应用程序的开发提供了综合.广泛的基础性支持的Java平台. Spring帮助开发人员攻克了开发中基础性的问 ...
- spring 配置属性的详细信息
摘要(这篇文章讲的红,蓝说这话节) 字面值 字面值:可用字符串表示的值,能够通过<value>元素标签或value属性进行注入 基本数据类型及其封装类.String等类型都能够採取字面值注 ...
随机推荐
- pwd命令(转)
Linux中用 pwd 命令来查看”当前工作目录“的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置. ...
- SQL Server Transaction Log Truncate && Shrink
目录 什么是事务日志 事务日志的组成 事务日志大小维护方法 Truncate Shrink 索引碎片 总结 什么是事务日志 Transaction log 是对数据库管理系统执行的一系列动作的记录 ...
- centos 下 django 1.8 配置好后 admin 后台无法显示 样式解决办法
解决前 解决命令 [root@ayibang-server static]# cat /etc/nginx/conf.d/office_djaong_uvpv.conf server { listen ...
- C# MySqlHelper
1.MySql官方提供ADO.NET访问模式的MySql.Data.dll,下载地址:http://dev.mysql.com/downloads/connector/net/ 2.MySqlHelp ...
- C语言深度剖析学习错误点记录
0. static修饰变量和函数 static修饰变量,1)限定作用域,本文件内.全局变量(自定义起,本文件前面要用需extern声明),局部变量函数内:2)生命周期,程序运行期间一直保存. stat ...
- sqlserver 2000事务复制问题
2000现在用的估计不多了,把之前收集的一些复制问题整理发布出来.可能都是些很白很二的问题,但人总是由最初的无知不断成长,不对之处欢迎指正. sqlserver 2000事务复制问题服务器A(发布) ...
- [转载]如何破解Excel VBA密码
原文链接:http://yhf8377.blog.163.com/blog/static/1768601772012102111032840/ 在此之前,先强调一下,这个方法只是用来破解Excel内部 ...
- rsync+sersync实时同步
A: 运行rsync daemonB: 运行sersync ,会监控目录,发现改变会更新推送到A上 rsync见上面rsync设置 sersync安装配置1.建立目录mkdir -p /opt/ser ...
- 从gitlab下载好cocoapods中遇到的问题
如果遇到 [!] Unable to satisfy the following requirements: - `Mantle (~> 2.0.3)` required by `Podfile ...
- views of postgresql user password and encrypted or unencrypted
password_encryption = onpostgres=# create user user1 with encrypted password 'user1';CREATE ROLEpost ...