Spring如何查找XSD?


近年来,www.springframework.org会被时不时的墙一下。如果在Spring配置文件中声明了XSD(如下),Spring会尝试加载此XSD,然后验证XML文件的正确性。

<?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

那如果www.springframework.org访问不到,Spring如何加载XSD来验证XML配置文件呢?

简短接说,Spring会注册一个自己的EntityResolver,然后当在XML文档中遇到XSD的systemID,也就是XSD文件的URL时,如:http://www.springframework.org/schema/beans/spring-beans.xsd,Spring的EntityResolver就会通过spring的jar包中META-INF/spring.schemas文件来做一个查找。Spring的2.0.1版本的jar包中的META-INF/spring.schemas文件内容如下:

http\://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd
http\://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd
http\://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd
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/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd
http\://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd
http\://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd

http\://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd
http\://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd
http\://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd
http\://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-2.0.xsd
http\://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd
http\://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd
http\://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd

注意,这个文件中有两类URL,一类带有版本号2.0,一类不带。如果在这个文件中能查找到,Spring将直接加载jar包中打包的XSD文件,而无需访问公网的www.springframework.org。

但是,如果你“悲剧”地在XSD的URL中写了版本号,但是这个版本号与classpath中spring的版本不相符合。比如,classpath中的spring的jar包版本是2.0.1,而你在XML文件中写的XSD的systemID是:http://www.springframework.org/schema/beans/spring-beans-2.5.xsd,那Spring就无法通过spring.schemas文件找映射到jar包中的XSD文件。就只能去公网上查找加载这个spring-beans-2.5.xsd文件。如果这时www.springframework.org刚好被墙了,访问不了,那么Spring容器就会报错,类似“Failed to read schema document”,容器就无法初始化成功了。

所以正确的写法应该是XSD的URL中不要加版本号(这样以后Spring的版本升级了,也不要紧了)。如果公司内部很多应用的Spring配置文件都已经在用加了版本号的XSD文件URL的话,那么一种比较土的解决方法是,自己搭建一个web服务器,把所有相关的XSD文件都放上去,然后在应用的机器hosts文件中,将www.springframework.org绑定到自己搭建的web服务器上去。这样,就不依赖公网的XSD文件了。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值