spring基础---->spring自定义标签(一)
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
友情链接
作者:
huhx
出处: www.cnblogs.com/huhx
格言:你尽力了,才有资格说自己的运气不好。
版权:本文版权归作者huhx和博客园共有,欢迎转载。未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
出处: www.cnblogs.com/huhx
格言:你尽力了,才有资格说自己的运气不好。
版权:本文版权归作者huhx和博客园共有,欢迎转载。未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库