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