相关概念
在使用spring的配置文件时,新添加一个配置文件就需要添加相应的约束,一直没有研究过为什么要有这些约束,这些约束是干什么的。spring在启动的时候需要验证xml文档,约束的作用就是来验证配置文件的xml文档语法的正确性。
在项目中其中的一个spring配置文件约束:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
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">
</beans>
XML文档的schema约束定义了文档的结构,内容和语法,元素和属性等,schema包括内容如下:
1.XML配置文件中所有的标签和属性都需要schema来定义
2.所有的schema都需要一个id,在XML配置文件中我们称之为namespace,其值是一个URI,通常是这个XML的xsd文件的地址
3.引入一个schema约束使用属性xmlns,属性值为对应schema文件的命名空间namespace
4.若引入的schema非w3c组织定义的,必须指定schema文件的位置,schema文件的位置由schemaLocation指定。
5.引入多个schema需要使用别名,xmlns:alias
配置文件解析:
1.beans:整个配置文件的根节点,包含一个或多个bean
2.:xmlns:context="http://www.springframework.org/schema/context"基本的命名空间
3.xsi:schemaLocation:将命名空间和模式位置关联,提供了一个xml namespace到对应的xsd文件的一个映射,所以在xsi:schemaLocation后面配置的字符串都是成对的。
4.使用别名引入多个schema文件