前景:常用一些依赖于Spring的组件时,发现能够经过自定义配置Spring的标签来实现插件的注入,例如数据库源的配置,Mybatis的配置等。那么这些Spring标签是如何自定义配置的?学习Spring标签的自定义配置为之后实现分布式服务框架作技术储备。spring
技术分析:Spring的标签配置是经过XML来实现的,经过XSD(xml Schema Definition)来定义元素,属性,数据类型等。数据库
Spring自定义标签解析app
1.Spring启动时经过扫描根目录下的META-INF文件下的spring.handlers和spring.schemas文件找处处理类所在的位置以及schemas的路径。框架
spring.handlers 分布式
http\://jewel.com/schema/jewel=com.jewel.spring.tag.handler.JewelNamespaceHandler
spring.schemaside
http\://jewel.com/schema/jewel.xsd=META-INF/jewel.xsd
2.实现命名空间的处理提供类(NamespaceHandlerSupport):这个类和spring.handlers中的路径对应学习
public class JewelNamespaceHandler extends NamespaceHandlerSupport { public void init() { registerBeanDefinitionParser("application", new AppBeanDefinitionParser(AppBeanCnf.class)); } }
3.实现解析类(AppBeanDefinitionParser)负责对标签的解析this
public class AppBeanDefinitionParser implements BeanDefinitionParser { private Class<?> clssze; public AppBeanDefinitionParser(Class<?> cls) { this.clssze = cls; } public BeanDefinition parse(Element element, ParserContext parserContext) { //其中Element类负责获取标签信息
//ParserContext是Spring上下文能够将bean注册到Spring中在这里负责注入工做 } }
4.XSD文件配置spa
jewel.xsd文件:改文件和spring.schemas中对应位置插件
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http://jewel.com/schema/jewel" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" targetNamespace="http://jewel.com/schema/jewel" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:import namespace="http://www.springframework.org/schema/beans" /> <xsd:element name="application"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="beans:identifiedType"> <xsd:attribute name="name" type="xsd:string" /> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> </xsd:schema>
5.在标签配置(beans.xml)
<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tool="http://www.springframework.org/schema/tool" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jewel="http://jewel.com/schema/jewel"-------------命名空间的指定 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://jewel.com/schema/jewel http://jewel.com/schema/jewel.xsd"----------路径指定> <!-- 注解支持 --> <jewel:application name="demo-app"></jewel:application>-------------------标签配置 </beans>
总结:经过以上步骤咱们就能够自定义application标签而且能够经过Sping解析。