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).
1 设计配置属性类,如下
package com.springcloud.schemax;
public class DataEntity {
private String pattern;
private boolean lenient;
private String ref;
private String name;
public String getRef() {
return ref;
}
public void setRef(String ref) {
this.ref = ref;
}
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
public boolean isLenient() {
return lenient;
}
public void setLenient(boolean lenient) {
this.lenient = lenient;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "DateEntity [pattern=" + pattern + ", lenient=" + lenient + ", ref=" + ref + ", name=" + name + "]";
}
}
2 编写XSD文件
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.mycompany.com/schema/myns"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://www.mycompany.com/schema/myns"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans" />
<xsd:element name="service">
<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:attribute name="ref" type="xsd:string" use="required" />
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
3 NamespaceHandler和BeanDefinitionParser完成解析工作
package com.springcloud.schemax;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class DataNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("service", new DataFormatBeanDefinitionParser());
}
}
package com.springcloud.schemax;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
public class DataFormatBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
protected Class<DataEntity> getBeanClass(Element element) {
return DataEntity.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.addPropertyValue("pattern", pattern);
String ref = element.getAttribute("ref");
bean.addPropertyValue("ref", ref);
String name = element.getAttribute("name");
bean.addPropertyValue("name", name);
// this however is an optional property
String lenient = element.getAttribute("lenient");
if (StringUtils.hasText(lenient)) {
bean.addPropertyValue("lenient", Boolean.valueOf(lenient));
}
}
}
4 Registering spring.handlers和spring.schemas
spring.handlers
http\://www.mycompany.com/schema/myns=com.springcloud.schemax.DataNamespaceHandler
spring.schemas
http\://www.mycompany.com/schema/myns/myns.xsd=META-INF/jcache2.xsd
5 测试
<?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:dubbox="http://www.mycompany.com/schema/myns"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.mycompany.com/schema/myns http://www.mycompany.com/schema/myns/myns.xsd">
<!-- as a top-level bean -->
<dubbox:service id="defaultDateFormat" pattern="yyyy-MM-dd HH:mm"
lenient="true" ref="this is test" name="hhhhh" />
</beans>
package com.springcloud.schemax;
import java.text.ParseException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApplication {
public static void main(String[] args) throws ParseException {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:application2.xml");
DataEntity xObject = context.getBean(DataEntity.class);
System.out.println(xObject);
}
}