基于Spring可扩展Schema提供自定义配置支持
2017-02-09 17:07
441 查看
spring 可以基于schema 扩展,自定义 schema。参考文档自己搭了个应用试验了一下:
首先看下自己写的 myns.xsd
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.yjhexy.com/schema/myns"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://www.yjhexy.com/schema/myns" 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>
然后看下我的applicationContxt.xml配置:
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:myns="http://www.yjhexy.com/schema/myns"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.yjhexy.com/schema/myns http://www.yjhexy.com/schema/myns/myns.xsd
">
<myns:dateformat id="dateFormat" pattern="yyyy-MM-dd HH:mm"
lenient="true" />
</beans>
很明显实现了个自定义的bean ,这个bean有两个属性,一个是时间的格式,另外一个不知道啥东西。
然后在META-INF下面写了两个文件,
spring.handlers:用来描述如何处理自定义的namespace
Xml代码
http\://www.yjhexy.com/schema/myns=com.yajun.balance.spring.MyNamespaceHandler
spring.schemas:描述schema的位置
Xml代码
http\://www.yjhexy.com/schema/myns/myns.xsd=com/yajun/balance/spring/myns.xsd
然后是需要两个类,一个处理namespace,一个处理beanDefinition
Java代码
package com.yajun.balance.spring;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class MyNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("dateformat", new SimpleDateFormatBeanDefinitionParser());
}
}
Java代码
package com.yajun.balance.spring;
import java.text.SimpleDateFormat;
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 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));
}
}
}
最后main输出试试:
Java代码
package com.yajun.balance.spring;
import java.text.SimpleDateFormat;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SimpleDateFormat f = (SimpleDateFormat) ctx.getBean("dateFormat");
System.out.println(f);
}
}
首先看下自己写的 myns.xsd
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.yjhexy.com/schema/myns"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://www.yjhexy.com/schema/myns" 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>
然后看下我的applicationContxt.xml配置:
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:myns="http://www.yjhexy.com/schema/myns"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.yjhexy.com/schema/myns http://www.yjhexy.com/schema/myns/myns.xsd
">
<myns:dateformat id="dateFormat" pattern="yyyy-MM-dd HH:mm"
lenient="true" />
</beans>
很明显实现了个自定义的bean ,这个bean有两个属性,一个是时间的格式,另外一个不知道啥东西。
然后在META-INF下面写了两个文件,
spring.handlers:用来描述如何处理自定义的namespace
Xml代码
http\://www.yjhexy.com/schema/myns=com.yajun.balance.spring.MyNamespaceHandler
spring.schemas:描述schema的位置
Xml代码
http\://www.yjhexy.com/schema/myns/myns.xsd=com/yajun/balance/spring/myns.xsd
然后是需要两个类,一个处理namespace,一个处理beanDefinition
Java代码
package com.yajun.balance.spring;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class MyNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("dateformat", new SimpleDateFormatBeanDefinitionParser());
}
}
Java代码
package com.yajun.balance.spring;
import java.text.SimpleDateFormat;
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 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));
}
}
}
最后main输出试试:
Java代码
package com.yajun.balance.spring;
import java.text.SimpleDateFormat;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SimpleDateFormat f = (SimpleDateFormat) ctx.getBean("dateFormat");
System.out.println(f);
}
}
相关文章推荐
- 基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
- 基于Spring可扩展Schema提供自定义配置支持
- 基于Spring可扩展Schema提供自定义配置支持
- Dubbo源码分析(三):自定义Schema--基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
- 基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
- 基于Spring可扩展Schema提供自定义配置支持
- 基于Spring可扩展Schema提供自定义配置支持
- 基于Spring可扩展Schema提供自定义配置支持
- 基于Spring可扩展Schema提供自定义配置支持
- 基于Spring可扩展Schema提供自定义配置支持
- 基于Spring可扩展Schema提供自定义配置支持
- 基于Spring可扩展Schema提供自定义配置支持
- 基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
- 基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
- 基于Spring可扩展Schema提供自定义配置支持(转载)
- 基于Spring可扩展Schema提供自定义配置支持
- 基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
- 基于Spring可扩展Schema提供自定义配置支持
- 基于Spring可扩展Schema提供自定义配置支持
- 基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)