前景:常用一些依賴於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解析。