今天在Spring配置文件中配置如下事务属性时,提示<tx is not bound(不受约束的),估计是配置文件的xsd没配置好。

<!-- 2.配置事务属性 -->
<tx:advice >
<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,可以看到如下页面:

Spring配置文件的xsd知识点

以上都是可选的tx的xsd版本,而xsi:schemaLocation的http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 在用浏览器访问是该版本xsd对应内容的页面:

 

 Spring配置文件的xsd知识点

箭头指的就是对<tx:advice进行xml校验的部分,从上面代码看出,tx还可以添加<tx:annotation-driven,<tx:jta-transaction-manager 节点,这个与xml中的提示是一致的:

Spring配置文件的xsd知识点

 

advice部分的具体代码如下:

 1 <xsd:element name="advice">
 2 <xsd:complexType>
 3 <xsd:annotation>
 4 <xsd:documentation source="java:org.springframework.transaction.interceptor.TransactionInterceptor">
 5 <![CDATA[
 6 Defines the transactional semantics of the AOP advice that is to be executed. That is, this advice element is where the transactional semantics of any    number of methods are defined (where transactional semantics includes the propagation settings, the isolation level, the rollback rules, and suchlike).
 7 ]]>
 8 </xsd:documentation>
 9 <xsd:appinfo>
10 <tool:annotation>
11 <tool:exports type="org.springframework.transaction.interceptor.TransactionInterceptor"/>
12 </tool:annotation>
13 </xsd:appinfo>
14 </xsd:annotation>
15 <xsd:complexContent>
16 <xsd:extension base="beans:identifiedType">
17 <xsd:sequence>
18 <xsd:element name="attributes" type="attributesType" minOccurs="0" maxOccurs="1"/>
19 </xsd:sequence>
20 <xsd:attribute name="transaction-manager" type="xsd:string" default="transactionManager">
21 <xsd:annotation>
22 <xsd:documentation source="java:org.springframework.transaction.PlatformTransactionManager">
23 <![CDATA[
24 The bean name of the PlatformTransactionManager that is to be used to drive transactions. This attribute is not required, and only needs to be specified explicitly if the bean name of the desired PlatformTransactionManager is not 'transactionManager'.
25 ]]>
26 </xsd:documentation>
27 <xsd:appinfo>
28 <tool:annotation kind="ref">
29 <tool:expected-type type="org.springframework.transaction.PlatformTransactionManager"/>
30 </tool:annotation>
31 </xsd:appinfo>
32 </xsd:annotation>
33 </xsd:attribute>
34 </xsd:extension>
35 </xsd:complexContent>
36 </xsd:complexType>
37 </xsd:element>
View Code

相关文章: