Spring具有一個基於架構的擴展機制,可以使用xml文件定義和配置bean。本博客將介紹如何編寫自定義XML bean的解析器,並用實例來加以說明。其實我一直相信 等你出現的時候我就知道是你。
Spring中標簽的拓展
自定義標簽大致分為以下幾個步驟:
1、Authoring an XML schema to describe your custom element(s). 2、Coding a custom NamespaceHandler implementation (this is an easy step, don’t worry). 3、Coding one or more BeanDefinitionParser implementations (this is where the real work is done). 4、Registering the above artifacts with Spring (this too is an easy step).
項目的結構如下:
一、定義一個schema,命名為huhx.xsd。內容如下:
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http://www.huhx.com/schema/ch" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" targetNamespace="http://www.huhx.com/schema/ch" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:import namespace="http://www.springframework.org/schema/beans" /> <xsd:element name="dateformat"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="beans:identifiedType"> <xsd:attribute name="lenient" type="xsd:boolean"/> <xsd:attribute name="pattern" type="xsd:string" use="required"/> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> </xsd:schema>
二、編寫一個NamespaceHandler,名為MyNamespaceHandler.java。
package com.linux.huhx.springDefined; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; /** * Created by huhx on 2017-05-17. */ public class MyNamespaceHandler extends NamespaceHandlerSupport { @Override public void init() { registerBeanDefinitionParser("dateformat", new SimpleDateFormatBeanDefinitionParser()); } }
三、編寫一個BeanDefinitionParser,名為SimpleDateFormatBeanDefinitionParser.java。
package com.linux.huhx.springDefined; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.util.StringUtils; import org.w3c.dom.Element; import java.text.SimpleDateFormat; /** * Created by huhx on 2017-05-17. */ public class SimpleDateFormatBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { protected Class getBeanClass(Element element) { return SimpleDateFormat.class; } protected void doParse(Element element, BeanDefinitionBuilder bean) { // this will never be null since the schema explicitly requires that a value be supplied String pattern = element.getAttribute("pattern"); bean.addConstructorArgValue(pattern); // this however is an optional property String lenient = element.getAttribute("lenient"); if (StringUtils.hasText(lenient)) { bean.addPropertyValue("lenient", Boolean.valueOf(lenient)); } } }
四、注冊上述的handler和schema
在META-INF下面創建兩個文件spring.handlers和spring.schemas,內容如下:
spring.handlers:
http\://www.huhx.com/schema/ch=com.linux.huhx.springDefined.MyNamespaceHandler
spring.schemas:
http\://www.huhx.com/schema/ch.xsd=huhx.xsd
五、在配置文件中我們測試使用自定義的標簽:huhx.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:ch="http://www.huhx.com/schema/ch" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.huhx.com/schema/ch http://www.huhx.com/schema/ch.xsd"> <ch:dateformat id="dateFormat" pattern="yyyy-MM-dd HH:mm" lenient="true"/> </beans>
六、測試的java類Main.java內容如下:
package com.linux.huhx.springDefined; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import java.text.SimpleDateFormat; import java.util.Date; /** * Created by huhx on 2017-05-17. */ public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("huhx.xml"); SimpleDateFormat info = (SimpleDateFormat) context.getBean("dateFormat"); System.out.println(info.format(new Date())); } }
打印的結果如下:
2017-05-17 17:32
友情鏈接
- spring自定義標簽的官方文檔:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#xml-custom