Spring具有一个基于架构的扩展机制,可以使用xml文件定义和配置bean。本博客将介绍如何编写自定义XML bean的解析器,并用实例来加以说明。其实我一直相信 等你出现的时候我就知道是你。

Spring中标签的拓展

自定义标签大致分为以下几个步骤:

  1. Authoring an XML schema to describe your custom element(s).
  2.  
  3. Coding a custom NamespaceHandler implementation (this is an easy step, dont worry).
  4.  
  5. Coding one or more BeanDefinitionParser implementations (this is where the real work is done).
  6.  
  7. Registering the above artifacts with Spring (this too is an easy step).

项目的结构如下:

一、定义一个schema,命名为huhx.xsd。内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsd:schema xmlns="http://www.huhx.com/schema/ch"
  3. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  4. xmlns:beans="http://www.springframework.org/schema/beans"
  5. targetNamespace="http://www.huhx.com/schema/ch"
  6. elementFormDefault="qualified"
  7. attributeFormDefault="unqualified">
  8.  
  9. <xsd:import namespace="http://www.springframework.org/schema/beans" />
  10.  
  11. <xsd:element name="dateformat">
  12. <xsd:complexType>
  13. <xsd:complexContent>
  14. <xsd:extension base="beans:identifiedType">
  15. <xsd:attribute name="lenient" type="xsd:boolean"/>
  16. <xsd:attribute name="pattern" type="xsd:string" use="required"/>
  17. </xsd:extension>
  18. </xsd:complexContent>
  19. </xsd:complexType>
  20. </xsd:element>
  21. </xsd:schema>

二、编写一个NamespaceHandler,名为MyNamespaceHandler.java。

  1. package com.linux.huhx.springDefined;
  2.  
  3. import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
  4.  
  5. /**
  6. * Created by huhx on 2017-05-17.
  7. */
  8. public class MyNamespaceHandler extends NamespaceHandlerSupport {
  9. @Override
  10. public void init() {
  11. registerBeanDefinitionParser("dateformat", new SimpleDateFormatBeanDefinitionParser());
  12. }
  13. }

三、编写一个BeanDefinitionParser,名为SimpleDateFormatBeanDefinitionParser.java。

  1. package com.linux.huhx.springDefined;
  2.  
  3. import org.springframework.beans.factory.support.BeanDefinitionBuilder;
  4. import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
  5. import org.springframework.util.StringUtils;
  6. import org.w3c.dom.Element;
  7.  
  8. import java.text.SimpleDateFormat;
  9.  
  10. /**
  11. * Created by huhx on 2017-05-17.
  12. */
  13. public class SimpleDateFormatBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
  14.  
  15. protected Class getBeanClass(Element element) {
  16. return SimpleDateFormat.class;
  17. }
  18.  
  19. protected void doParse(Element element, BeanDefinitionBuilder bean) {
  20. // this will never be null since the schema explicitly requires that a value be supplied
  21. String pattern = element.getAttribute("pattern");
  22. bean.addConstructorArgValue(pattern);
  23.  
  24. // this however is an optional property
  25. String lenient = element.getAttribute("lenient");
  26. if (StringUtils.hasText(lenient)) {
  27. bean.addPropertyValue("lenient", Boolean.valueOf(lenient));
  28. }
  29. }
  30. }

四、注册上述的handler和schema

在META-INF下面创建两个文件spring.handlers和spring.schemas,内容如下:

spring.handlers:

  1. http\://www.huhx.com/schema/ch=com.linux.huhx.springDefined.MyNamespaceHandler

spring.schemas:

  1. http\://www.huhx.com/schema/ch.xsd=huhx.xsd

五、在配置文件中我们测试使用自定义的标签:huhx.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:ch="http://www.huhx.com/schema/ch"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  7. http://www.huhx.com/schema/ch
  8. http://www.huhx.com/schema/ch.xsd">
  9. <ch:dateformat id="dateFormat" pattern="yyyy-MM-dd HH:mm" lenient="true"/>
  10. </beans>

六、测试的java类Main.java内容如下:

  1. package com.linux.huhx.springDefined;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import org.springframework.context.support.FileSystemXmlApplicationContext;
  6.  
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9.  
  10. /**
  11. * Created by huhx on 2017-05-17.
  12. */
  13. public class Main {
  14. public static void main(String[] args) {
  15. ApplicationContext context = new ClassPathXmlApplicationContext("huhx.xml");
  16. SimpleDateFormat info = (SimpleDateFormat) context.getBean("dateFormat");
  17. System.out.println(info.format(new Date()));
  18. }
  19. }

打印的结果如下:

  1. -- :

友情链接

  

spring基础---->spring自定义标签(一)的更多相关文章

  1. spring整合freemarker 自定义标签

    1.自定义标签实现 TemplateDirectiveModel 接口 2.spring 配置,注意标红的两行 <bean id="freemarkerConfig" cla ...

  2. spring中的自定义标签

    为了给系统提供可配置化支持,一般会用原生态的方式去解析定义好的XML文件,然后转化为配置对象.这种方式对于简单.单一的配置文件,或者是XML配置格式固定的配置文件,比较容易处理.但是对于一些配置非常复 ...

  3. spring源码-自定义标签-4

    一.自定义标签,自定义标签在使用上面相对来说非常常见了,这个也算是spring对于容器的拓展.通过自定义标签的方式可以创造出很多新的配置方式,并且交给容器直接管理,不需要人工太多的关注.这也是spri ...

  4. Spring 源码(4)在Spring配置文件中自定义标签如何实现?

    Spring 配置文件自定义标签的前置条件 在上一篇文章https://www.cnblogs.com/redwinter/p/16165274.html Spring BeanFactory的创建过 ...

  5. JavaWeb基础—JSP自定义标签入门

    自定义标签的作用:替换JSP页面的java代码 步骤:1.标签处理类(标签是一个对象,那也就需要先有类) 2.tld文件 它是一个xml(可以向c标签里借),一般放到WEB-INF下,不让客户端浏览器 ...

  6. spring基础---->spring自定义初始化(二)

    这里新增了对ref属性的支持,并且过滤了已经解析的元素.人生有两个词很棒,一言不合和不提也罢. spring自定义对ref属性支持 项目的结构如下:新增一个ThirdBean类,修改了ParseXml ...

  7. spring基础---->spring自定义初始化(一)

    这里我们简单的实现一下spring中的初始化bean,以大概了解他的流程.受委屈几乎是一个人成长最快的途径,吃下去的是委屈,消化掉后得到的是格局. spring的自定义初始化 测试的项目结构如下: 一 ...

  8. Spring 源码学习(1) —— 自定义标签

    Spring 工作流程是先加载解析xml配置文件:配置文件中存在默认的标签,也可以自定义标签.解析默认标签调用: private void parseDefaultElement(Element el ...

  9. spring基础学习01

    spring基础 Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用 IOC控制反转 把创建对象和维护对象之间的关系权利 ...

随机推荐

  1. 微信小程序请求wx.request数据,渲染到页面

    先说一下基本使用.官网也有. 比如说你在App.js里面有这些变量.想修改某些值. data: { main_view_bgcolor: "", border: "&qu ...

  2. php比较函数,判断安全函数

    一.字符串比较函数: int strcasecmp ( string $str1 , string $str2 ) int strcmp ( string $str1 , string $str2 ) ...

  3. spring容器ApplicationContext初始化(spring应用上下文初始化)

    可以通过以下三种方式加载spring容器,实现bean的扫描与管理: 1. ClassPathXmlApplicationContext:从类路径中加载 ClassPathXmlApplication ...

  4. RVM(ruby version manage)安装指南

    一.安装需要3步gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 \c ...

  5. [k8s]metricbeat的kubernetes模块&kube-metric模块

    正确姿势启动metricbeat metricbeat.modules: - module: system metricsets: - cpu - filesystem - memory - netw ...

  6. Java线程停止interrupt()方法

    程序是很简易的.然而,在编程人员面前,多线程呈现出了一组新的难题,如果没有被恰当的解决,将导致意外的行为以及细微的.难以发现的错误.在本篇文章中,我们针对这些难题之一:如何中断一个正在运行的线程. 中 ...

  7. Redis 面试题(持续更新)

    前言 看了一圈,发现Redis的面试题主要问的是如下几块: 原理 用处(缓存/队列 包括Pub.Sub/计数器/排行榜等) 基本操作与数据类型 消息队列 且与其它消息队列的区别 主从备份 宕机如何处理 ...

  8. 1.3 Seven Testing Principles

    1.3 Seven Testing Principles 2015-06-23 Principle 1 - Testing shows presence of defects(测试显示存在缺陷) Te ...

  9. Ubuntu 15.04 开启远程桌面

    1.安装Xrdp Windows远程桌面使用的是RDP协议,所以ubuntu上就要先安装Xrdp,终端命令行输入安装: sudo apt-get install xrdp vnc4server xba ...

  10. Keil的使用方法(汇总)

    推荐 分享一个大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到人工智能的队伍中来! http://www.captainbed.net/strongerhuang 软件的开发 ...