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

Spring中标签的拓展

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

、Authoring an XML schema to describe your custom element(s). 

、Coding a custom NamespaceHandler implementation (this is an easy step, don’t worry).

、Coding one or more BeanDefinitionParser implementations (this is where the real work is done).

、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()));
}
}

打印的结果如下:

-- :

友情链接

  

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. SQL server 存储过程 C#调用Windows CMD命令并返回输出结果 Mysql删除重复数据保留最小的id C# 取字符串中间文本 取字符串左边 取字符串右边 C# JSON格式数据高级用法

    create proc insertLog@Title nvarchar(50),@Contents nvarchar(max),@UserId int,@CreateTime datetimeasi ...

  2. DPDK 全面分析

    本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. 高性能网络技术 ...

  3. Out of Hay(poj2395)(并查集)

    Out of Hay Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11580   Accepted: 4515 Descr ...

  4. 移动端rem的用法

    标签: 1.rem(font size of the root element)是指相对于根元素的字体大小的单位,em(font size of the element)是指相对于父元素的字体大小的单 ...

  5. Mac下Selenium无法最大化Chrome解决方案

    在用Selenium做自动化操作时,一般最大化浏览器的代码都是:driver.manage().window().maximize(), 但是在Mac下这样是无法最大化Chrome浏览器的,解决方法: ...

  6. nodeJs should+mocha+istanbul 测试 遇到的坑

    .istanbul 和 mocha 结合进行nodejs测试的时候最后执行 istanbul cover _mocha test.sqrt.js的时候报错 如图: 用 ../node_modules/ ...

  7. WPF集合

    Dependency Property 依赖属性 http://www.cnblogs.com/HelloMyWorld/archive/2013/02/21/2920149.html Attache ...

  8. JS PHP MySQL 字符长度

    摘要: js的string.length 属性取的是字符串的实际长度 php的str_len()函数取的是字符串的字节长度,中文utf-8占3个字节,gb2312占2个字节 mysql中的varcha ...

  9. 一款基于jQuery的图片下滑切换焦点图插件

    之前为大家分享了好多款jquery插件,今天我们要分享的一款jQuery插件也比较实用,是一款jQuery焦点图插件.焦点图相当普通,一共可以循环播放4张图片,并且每一张图片在切换的时候都是向下滑动的 ...

  10. HDFS的实现机制

    参考以上这张图,实际上我们客户端访问HDFS里面的内容时,并不需要真实知道内容存在于服务器的内容的真实路径,我们只需要知道一个虚拟路径就可以,比如最上面的hdfs://weekend110:9000/ ...