spring自定义标签实现
来源:互联网 发布:软件平台建设规划 编辑:程序博客网 时间:2024/05/07 10:59
Spring中,除了使用官方提供的bean等标签外,也提供了方便开发者自定义标签的实现方式,废话少续,直接上步骤:
- 创建schema文件(myns.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="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>
- 创建命名空间处理类(MyNameSpaceHandler.java):
package spring.extensibletag;import org.springframework.beans.factory.xml.NamespaceHandlerSupport;public class MyNameSpaceHandler extends NamespaceHandlerSupport {@Overridepublic void init() {registerBeanDefinitionParser("dateformat", new SimpleDateFormatBeanDefinitionParser());}}
- 创建自定义bean的parser类:
package spring.extensibletag;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 {@Overrideprotected Class<?> getBeanClass(Element element) {return SimpleDateFormat.class;}@Overrideprotected 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.addConstructorArg(pattern); // this however is an optional property String lenient = element.getAttribute("lenient"); if (StringUtils.hasText(lenient)) { bean.addPropertyValue("lenient", Boolean.valueOf(lenient)); } }}
- 注册处理类和命名空间定义schema文件(spring.handlers和spring.schemas):
spring.handlershttp\://www.mycompany.com/schema/myns=spring.extensibletag.MyNameSpaceHandler
spring.schemashttp\://www.mycompany.com/schema/myns/myns.xsd=myns.xsd
- 在spring的applicationContext.xml文件中定义bean:
<?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.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"><myns:dateformat id="defaultDateFormat" pattern="yyyy-MM-dd HH:mm" lenient="true" /></beans>
- 使用示例:
package spring.extensibletag;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {@SuppressWarnings("resource")public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");SimpleDateFormat format = (SimpleDateFormat) context.getBean("defaultDateFormat");System.out.println(format.format(new Date()));}}
该示例所在项目的文件目录如下图所示:
0 0
- spring自定义标签实现
- spring自定义标签实现
- spring自定义标签实现
- spring自定义标签-自我实现
- spring自定义标签如何实现
- spring自定义标签aop实现
- spring自定义缓存标签的实现
- Spring进阶之自定义标签实现
- Spring进阶之自定义标签实现
- spring自定义标签
- spring 自定义标签 学习
- 自定义Spring配置标签
- spring自定义标签
- spring自定义标签
- spring自定义标签
- Spring自定义标签解析
- spring 自定义标签
- Spring 自定义标签
- PAT (Basic Level)1024. 科学计数法
- libpq简单示例
- Storm里executor数量如何产生,acker如何分配的
- 上海1套房8个月涨500万 购房者:干30年不如买房
- iOS WebView与h5
- spring自定义标签实现
- 【总结】PHP常见面试题汇总(三)
- 图像验证码识别(五)——去除噪点
- Android Intent调用大全、系统自带Intent调用大全
- unity中事件分发系统 EventDispatcher
- Exercise #4
- C# Lambda表达式、匿名、回调
- WIFI基本知识整理
- iFly Mobile Speech Platform Mobile Speech Client 5.0(移动语音讯飞平台开发手册)