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. C# BeginInvoke和EndInvoke方法

    转载自:BeginInvoke和EndInvoke方法 IDE:Visual Studio 2008 本系列教程主要包括如下内容:1. BeginInvoke和EndInvoke方法 2. Threa ...

  2. NHibernate中ISession的Flush

    不知道在执行Insert或者Delete,update之后为什么要调用Flush(),后来看了http://www.cnblogs.com/lyj/archive/2008/10/17/1313612 ...

  3. mysql string types ---- mysql 字符类型详解

    一.mysql 中包涵的字符类型: [national] char [(m)] [character set charset_name] [collate collation_name] [natio ...

  4. mysql在linux7下systemd的相关配置

    ---- # Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. # # This program ...

  5. Atitit.软件gui按钮and面板---通讯子系统(区)-- github 的使用....

    Atitit.软件gui按钮and面板---通讯子系统(区)-- github 的使用.... 1. 1.注册账户以及创建仓库 1 2. 二.在GitHub中创建项目(create a new rep ...

  6. Tomcat7中开启gzip压缩功能的配置方法

    使用gzip压缩可以减少数据传输大小,加快网页加载速度.很多大站都开启了gzip压缩,不过也有很多网站并没有开启gzip压缩,上次看了一篇文章说开启gzip压缩后对搜索引擎不友好,但从带宽和流量的角度 ...

  7. socket发送http请求

  8. cpython和lua源码阅读

    cpython代码很多,不太容易看出来. lua代码真的短小精悍,不得不佩服.

  9. spring 注解@Resource @Autowired区别

    1.@Autowired寻找类的时候默认是ByType,也就是通过类的类型来寻找类.不过,也可以通过借助@Qualifier("name")来指定寻找的类名 @Autowired ...

  10. Android——Android studio项目中如何查看R.java文件(转)

    Android Studio 是Google推出的一个Android开发环境,它集成了Android 开发工具用于开发和调试,类似 Eclipse ADT.Google公司停止对eclipse的后续支 ...