1. 程式人生 > 其它 >Spring解析自定義標籤

Spring解析自定義標籤

Spring解析自定義標籤

  • Spring的XML標籤按照解析方式劃分,可以分為兩類:預設名稱空間標籤和自定義標籤。預設名稱空間一共有四個:beans bean import alias,其他的標籤全部叫做自定義標籤

  • 一個標籤的解析載入一共需要經過如下幾個流程:

    • 讀取XML配置檔案,得到String[] configLocations
    • 遍歷configLocations,將每一個location轉為Resource resource
    • 將resource轉成Document doc
    • 遍歷Element中的每一個node節點,如果是預設名稱空間呼叫parseDefaultElement()方法,如果是自定義標籤則會呼叫delegate.parseCustomElement()方法。
  • Spring解析自定義標籤需要的資訊有:Entity、BeanDefinitionParser、NamespaceHandler、spring.schemas、spring.handlers、xsd檔案、xml檔案、載入啟動類ClassPathXmlApplicationContext

  • Spring解析自定義標籤的實際流程封裝的比較深,需要從refresh()中的obtainFreshBeanFactory()方法作為入口,慢慢深入探索。只要細心的捋一遍內部實現就能搞明白,實打實的紙老虎,不難的。

自定義Entity資訊(注意需要新增set和get方法)

@Data
public class Deer {

    private String worker;

    private String country;

    private String color;

}

定義BeanDefinitionParser

public class DeerBeanDefinitionParser implements BeanDefinitionParser {
    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {
        String address = element.getAttribute("address");
        String country = element.getAttribute("country");
        String color = element.getAttribute("color");

        System.out.println("address is :" + address);
        System.out.println("country is :" + country);
        System.out.println("color is :" + color);
        return null;
    }
}

定義NamespaceHandler

public class DeerNamespaceHandler extends NamespaceHandlerSupport {

    @Override
    public void init() {
        registerBeanDefinitionParser("deer", new DeerBeanDefinitionParser());
    }
}

建立spring.schemas,spring.schemas檔案需要放置在resources/META-INF下才能被Spring掃描到

http\://www.deerhang.org/schema/deer.xsd=xml/deer.xsd

建立spring.handlers,放置位置同spring.shemas

http\://www.deerhang.org/schema/deer=com.xxx.mine.DeerNamespaceHandler

建立xsd檔案,xsd檔案必須放在resource下,路徑對應spring.schames中的xsd位置

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<xsd:schema xmlns="http://www.deerhang.org/schema/deer"
		xmlns:xsd="http://www.w3.org/2001/XMLSchema"
		targetNamespace="http://www.deerhang.org/schema/deer">
	<xsd:element name="deer">
		<xsd:complexType>
			<xsd:attribute name="country" type="xsd:string"></xsd:attribute>
			<xsd:attribute name="color" type="xsd:string"></xsd:attribute>
			<xsd:attribute name="address" type="xsd:string"></xsd:attribute>
		</xsd:complexType>
	</xsd:element>
</xsd:schema>

建立xml檔案,用來定義配置資訊,xml檔案同樣放置在resource下applicationContext.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:hang="http://www.deerhang.org/schema/deer"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.deerhang.org/schema/deer http://www.deerhang.org/schema/deer.xsd">
    <hang:deer address="清原" color="黃色" country="中國"></hang:deer>
</beans>

最後一步配置啟動類ClassPathXmlApplicationContext

public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

    }