spring基础---->spring自定义标签(一)
2023-09-27 14:20:11 时间
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 框架基础(06):Mvc架构模式简介,执行流程详解
- Spring 框架基础(04):AOP切面编程概念,几种实现方式演示
- Spring表达式语言 之 5.1 概述 5.2 SpEL基础(拾叁)
- CSDN日报191120:《吊打面试官》系列-消息队列基础;Vue + Spring Boot 项目实战
- CSDN日报191018:记软件开发满五年;Spring Boot 2.2 正式发布
- Spring Boot整合tk.Mapper/通用Mapper
- Spring Cloud 全链路日志追踪实现
- spring batch(一):基础部分
- Java回顾之Spring基础
- Spring Security测试代码
- Spring Security 实战干货:OAuth2授权请求是如何构建并执行的
- Spring官方宣布:新的Spring OAuth2.0授权服务器已经来了
- 你知道如何自动保存 Spring Boot 应用进程号吗
- Spring Boot 2.X系列教程:七天从无到有掌握Spring Boot-持续更新
- Spring官方文档翻译——15.4 处理器映射(Handler mappings)
- Spring知识点提炼