Spring学习笔记(三)基于XML Schema的配置方式

本文介绍了Spring从2.0开始引入的基于XML Schema的配置方式,以简化Spring配置文件。详细讲解了Spring Schema机制,包括XML校验、NamespaceHandler和BeanDefinitionParser的工作原理。重点讨论了p-namespace、c-namespace的使用,以及util schema在配置集合、引用常量等方面的应用。此外,还提到了其他常用的Spring Schema,如aop、jee、jms等。
摘要由CSDN通过智能技术生成

前言:Spring2.0开始,Spring允许使用基于XML Schema的配置方式来简化Spring配置文件,这种方式更加简洁,可以对Spring配置文件进行“减肥”。
  Spring配置文件的基本配置的<beans>标签包含如下配置:

<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
</beans>

  其中已经包含了一个最基本的Schema:spring-beans-4.0.xsd,也是默认的命名空间。如果对XML命名空间不了解的可以从参考:XML学习笔记

一、Spring中的Schema机制解析

  Spring在启动时是要检验XML文件的,如果我们打开上面的xmlns后面的链接,不难发现那是保存各个版本spring-beans-*.xsd的远程文档。如果断网,name就无法访问这些文件了,Spring已经将此情况考虑进去了,Spring默认从本地加载XSD文件。

  首先说明Spring创建Schema的基本过程:

  1. 设计配置属性和JavaBean。也就是设计好配置项,并通过JavaBean来建模。
  2. 编写XSD文件。(详细可参考:Schema 教程
  3. 编写NamespaceHandler和BeanDefinitionParser完成解析工作
  4. 编写spring.handlers和spring.schemas串联起所有部件
  5. 在Bean文件中应用。

  下面重点说明一下第四/五步,第四步是编写NamespaceHandler和BeanDefinitionParser完成解析工作,具体说来 NamespaceHandler会根据schema和节点名找到某个BeanDefinitionParser,然后由 BeanDefinitionParser完成具体的解析工作,开发好的handler与xsd还没法让应用感知到,这就需要将它们串联起来。第五步是编写spring.handlers和spring.schemas串联起所有部件,这两个文件的地址必须是META-INF/spring.handlers和META-INF/spring.schemas,spring会默认去 载入它们

<1> spring.handlers

  spring.handlers文件官网解释:The properties file called 'spring.handlers' contains a mapping of XML Schema URIs to namespace handler classes.

http\://www.mycompany.com/schema/myns=org.springframework.samples.xml.MyNamespaceHandler

  The ':' character is a valid delimiter in the Java properties format, and so the':' character in the URI needs to be escaped with a backslash.
  The first part (the key) of the key-value pair is the URI associated(关联) with your custom(自定义) namespace extension(拓展), and needs to match(匹配) exactly(精确地) the value of the'targetNamespace'attribute(属性) as specified in your custom XSD schema.

  也就是说spring.handlers文件保存的是XML Schema的URI与NamespaceHandler类的映射关系,这是一个property文件,因此是kye-value的形式,其中key是我们在XSD Schema中'targetNamespace'属性所指定的命名空间的名字,其中冒号需要转义,而其对应的值就是解析这个命名空间的NamespaceHandler。

<2> spring.schemas

  spring.schemas文件官网解释:The properties file called 'spring.schemas' contains a mapping of XML Schema locations(referred to along with the schema declaration in XML files that use the schema as partof the'xsi:schemaLocation' attribute) to classpath resources. This file is neededto prevent Spring from absolutely having to use a defaultEntityResolver that requiresInternet access to retrieve the schema file. If you specify the mapping in thisproperties file, Spring will search for the schema on the classpath (in this case'myns.xsd' in the'org.springframework.samples.xml' package):

http\://www.mycompany.com/schema/myns/myns.xsd=org/springframework/samples/xml/myns.xsd

  The upshot of this is that you are encouraged to deploy your XSD file(s) right alongsidetheNamespaceHandler andBeanDefinitionParser classes on the classpath.
  也就是说spring.handlers包含的是XML Schema文件本地存储的映射关系,其键值对中的值都可以在文件中找到。这个文件中保存的键都是可以在引用的XML文件中的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值