spring配置文件中xsd引用问题
今天在Spring配置文件中配置如下事务属性时,提示<tx is not bound(不受约束的),估计是配置文件的xsd没配置好。
<!-- 2.配置事务属性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true"></tx:method> <tx:method name="*"></tx:method> </tx:attributes> </tx:advice>
在xsi:schemaLocation=中增加:
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
错误就消失了。
网上查阅了相关资料,现总结如下:
xml文档要有格式,为了Spring的配置文件增加的节点(比如<tx:advice)能符合要求、合法,必须通过引入校验该xml格式的文件,也就是xsd文件。Spring通过配置可以联网引入xsd文件,也可以在断网下使用本地xsd文件校验xml文件。
xsi:schemaLocation 属性提供一种方法来查找在 XML 实例文档中定义的命名空间的 XML 架构定义。它的值是用空白分隔的统一资源标识符 (URI) 对的列表,其中的每一对 URI 都依次包含一个命名空间以及该命名空间的 XML 架构定义(通常为 .xsd 文件)的位置。
xsi: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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Spring在启动时默认要加载xsd文件来验证XML文件的,在浏览器中输入复制的xmlns:tx的链接,也就是http://www.springframework.org/schema/tx,可以看到如下页面:
以上都是可选的tx的xsd版本,而xsi:schemaLocation的http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 在用浏览器访问是该版本xsd对应内容的页面:
箭头指的就是对<tx:advice进行xml校验的部分,从上面代码看出,tx还可以添加<tx:annotation-driven,<tx:jta-transaction-manager 节点,这个与xml中的提示是一致的:
advice部分的具体代码如下:

如果此时无法上网,就无法从该网站获取到该xsd文件,就无法对xml文件进行校验了,容易发生应用启动不了的情况。为此我们可以加载本地的xsd文件,这样断网情况下也可以启动应用了。举例子来说,若tx的xsd文件没有配置,则要进行如下步骤进行配置:
1、点开项目的lib,找到spring-tx-4.1.4.RELEASE.jar,右键属性-复制文件路径(复制jar之前的路径),在文件夹地址栏粘贴,回车打开;
2、将spring-tx-4.1.4.RELEASE.jar解压到某个文件夹,有如下文件:
3、在org\springframework\transaction\config路径下可以找到:
里面还有旧版本的xsd文件,这样可以避免配置文件用的还是旧版本的xsd文件,而Spring是新版本情况下,断网应用启动不了的情况。Spring这个确实想得挺周到的。
4、将tx的某个版本的xsd文件拷贝到工程的source folder下,然后xml的配置改成如下形式:
xsi:schemaLocation="http://www.springframework.org/schema/beans
classpath:beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/mvc
classpath:mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/context
classpath:context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
classpath:aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
classpath:tx/spring-tx-4.1.xsd ">
这样就可以从本地获取xsd文件,就算断网也可以解析xml文件了。
附一份常见的spring配置文件的文件头:
------------------------------------3.0----------------------------
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
</beans>
当然你也可以根据使用的Spring的相应jar包版本更改对应的版本,比如上面的3.0可以改成4.1也行。
http://www.springframework.org/schema/ 该网站是Spring所有可能用到的结点属性的xsd列表,点击相应选项即可查到对应的xsd版本与xsd的URI:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
· Windows 提权-UAC 绕过
2017-12-16 java去除数组中重复的元素方法总结
2017-12-16 Java锁的设计
2017-12-16 mysql递归查询子类ID查询所有子类
2017-12-16 mysql递归查询从子类ID查询所有父类
2017-12-16 Java 正则表达式格式化时间显示
2017-12-16 java容器HashMap原理
2017-12-16 java容器的总结