Spring文件中的xsd文件

分享知识 传递快乐


Spring文件中的引用的xsd文件是用于校验xml文件的格式用的。

Spring是如何校验XML的:
Spring默认在启动时是要加载XSD文件来验证xml文件的,所以如果有的时候断网了,或者一些开源软件切换域名,那么就很容易碰到应用启动不了。为了防止这种情况,Spring提供了一种机制,默认从本地加载XSD文件。

例如:可以打开spring-context-4.3.0.RELEASE.jar,进入org/springframework/context/config/目录,可以看到下面有
spring-context-xxx.xsd
spring-context-xxx.xsd
spring-context-xxx.xsd
spring-context-xxx.xsd
......

Spring中的xsd文件带版本号与不带版本号的区别:
不带版本号,它会自动使用JAR中最新的xsd;这样以后升级Spring版本的时候,你的配置文件不用再手动改动。Spring的配置里,最好不要配置xsd文件的版本号。

另外如果没有配置版本号,取的就是当前jar里的XSD文件,减少了各种风险。其实,最大的好处是,当你升级了Spring的jar,该配置文件的XSD声明部分也不用改,会自动找到最新的本地jar里的XSD声明。Spring做了特殊处理,保证断网的情况下不写XSD版本号,能找到本地的jar文件。


完整的

<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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
  xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	   http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
	   http://www.springframework.org/schema/aop
	   http://www.springframework.org/schema/aop/spring-aop.xsd
	   http://www.springframework.org/schema/mvc
	   http://www.springframework.org/schema/mvc/spring-mvc.xsd
	   http://www.springframework.org/schema/tx
	   http://www.springframework.org/schema/tx/spring-tx.xsd
	   http://www.springframework.org/schema/task
	   http://www.springframework.org/schema/task/spring-task.xsd">
</beans>

MVC

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


AOP

<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" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	   http://www.springframework.org/schema/beans/spring-beans.xsd
	   http://www.springframework.org/schema/aop
	   http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>


TX

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

TASK

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


如果用到的其它配置信息可以引用

<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" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	   http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
</beans>



例:

如果在启动项目时报:

org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 76; 元素 "context:property-placeholder" 的前缀 "context" 未绑定。

追溯到配置文件中,如果是

<context:property-placeholder location="classpath:config/db.properties" />

引起的,那么要bean头部加入以下信息即可:

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



  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
对于Spring框架的XML配置,可以使用XSDXML Schema Definition)文件来定义和验证配置文件的结构和内容。XSD文件定义了配置文件可以使用的元素、属性和它们之间的关系。 在Spring框架,可以使用以下步骤来配置XSD: 1. 首先,确保你已经包含了Spring的命名空间定义和相关的Schema Location。例如,你可以在XML文件的顶部添加以下命名空间声明: ```xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" ``` 还需要提供Schema Location,指定XSD文件的位置,例如: ```xml xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" ``` 2. 然后,在配置文件的根元素使用Spring的命名空间和相应的元素。例如,要配置Spring的上下文,可以使用`<context:component-scan>`元素: ```xml <context:component-scan base-package="com.example" /> ``` 3. 最后,根据具体的需求,在配置文件使用其他Spring元素和属性进行配置。根据XSD文件的定义,这些元素和属性应该符合预先定义的规则。 通过这些步骤,你可以使用XSD文件配置Spring框架的XML文件,确保配置的正确性和一致性。请注意,具体的XSD文件和命名空间可能会因不同的Spring版本而有所不同,所以请根据你使用的Spring版本来选择合适的XSD文件和命名空间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旷野历程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值