Spring 自定義標簽配置


前景:經常使用一些依賴於Spring的組件時,發現可以通過自定義配置Spring的標簽來實現插件的注入,例如數據庫源的配置,Mybatis的配置等。那么這些Spring標簽是如何自定義配置的?學習Spring標簽的自定義配置為以后實現分布式服務框架做技術儲備。

技術分析:Spring的標簽配置是通過XML來實現的,通過XSD(xml Schema Definition)來定義元素,屬性,數據類型等。

Spring自定義標簽解析

1.Spring啟動時通過掃描根目錄下的META-INF文件下的spring.handlers和spring.schemas文件找到處理類所在的位置以及schemas的路徑。

       spring.handlers                             

http\://jewel.com/schema/jewel=com.jewel.spring.tag.handler.JewelNamespaceHandler

  spring.schemas

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)負責對標簽的解析

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文件配置

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解析。

 

 

         

 

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM