spring基础---->spring自定义标签(一)
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自定义标签的官方文档:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#xml-custom
spring基础---->spring自定义标签(一)的更多相关文章
- spring整合freemarker 自定义标签
1.自定义标签实现 TemplateDirectiveModel 接口 2.spring 配置,注意标红的两行 <bean id="freemarkerConfig" cla ...
- spring中的自定义标签
为了给系统提供可配置化支持,一般会用原生态的方式去解析定义好的XML文件,然后转化为配置对象.这种方式对于简单.单一的配置文件,或者是XML配置格式固定的配置文件,比较容易处理.但是对于一些配置非常复 ...
- spring源码-自定义标签-4
一.自定义标签,自定义标签在使用上面相对来说非常常见了,这个也算是spring对于容器的拓展.通过自定义标签的方式可以创造出很多新的配置方式,并且交给容器直接管理,不需要人工太多的关注.这也是spri ...
- Spring 源码(4)在Spring配置文件中自定义标签如何实现?
Spring 配置文件自定义标签的前置条件 在上一篇文章https://www.cnblogs.com/redwinter/p/16165274.html Spring BeanFactory的创建过 ...
- JavaWeb基础—JSP自定义标签入门
自定义标签的作用:替换JSP页面的java代码 步骤:1.标签处理类(标签是一个对象,那也就需要先有类) 2.tld文件 它是一个xml(可以向c标签里借),一般放到WEB-INF下,不让客户端浏览器 ...
- spring基础---->spring自定义初始化(二)
这里新增了对ref属性的支持,并且过滤了已经解析的元素.人生有两个词很棒,一言不合和不提也罢. spring自定义对ref属性支持 项目的结构如下:新增一个ThirdBean类,修改了ParseXml ...
- spring基础---->spring自定义初始化(一)
这里我们简单的实现一下spring中的初始化bean,以大概了解他的流程.受委屈几乎是一个人成长最快的途径,吃下去的是委屈,消化掉后得到的是格局. spring的自定义初始化 测试的项目结构如下: 一 ...
- Spring 源码学习(1) —— 自定义标签
Spring 工作流程是先加载解析xml配置文件:配置文件中存在默认的标签,也可以自定义标签.解析默认标签调用: private void parseDefaultElement(Element el ...
- spring基础学习01
spring基础 Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用 IOC控制反转 把创建对象和维护对象之间的关系权利 ...
随机推荐
- 微信小程序请求wx.request数据,渲染到页面
先说一下基本使用.官网也有. 比如说你在App.js里面有这些变量.想修改某些值. data: { main_view_bgcolor: "", border: "&qu ...
- php比较函数,判断安全函数
一.字符串比较函数: int strcasecmp ( string $str1 , string $str2 ) int strcmp ( string $str1 , string $str2 ) ...
- spring容器ApplicationContext初始化(spring应用上下文初始化)
可以通过以下三种方式加载spring容器,实现bean的扫描与管理: 1. ClassPathXmlApplicationContext:从类路径中加载 ClassPathXmlApplication ...
- RVM(ruby version manage)安装指南
一.安装需要3步gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 \c ...
- [k8s]metricbeat的kubernetes模块&kube-metric模块
正确姿势启动metricbeat metricbeat.modules: - module: system metricsets: - cpu - filesystem - memory - netw ...
- Java线程停止interrupt()方法
程序是很简易的.然而,在编程人员面前,多线程呈现出了一组新的难题,如果没有被恰当的解决,将导致意外的行为以及细微的.难以发现的错误.在本篇文章中,我们针对这些难题之一:如何中断一个正在运行的线程. 中 ...
- Redis 面试题(持续更新)
前言 看了一圈,发现Redis的面试题主要问的是如下几块: 原理 用处(缓存/队列 包括Pub.Sub/计数器/排行榜等) 基本操作与数据类型 消息队列 且与其它消息队列的区别 主从备份 宕机如何处理 ...
- 1.3 Seven Testing Principles
1.3 Seven Testing Principles 2015-06-23 Principle 1 - Testing shows presence of defects(测试显示存在缺陷) Te ...
- Ubuntu 15.04 开启远程桌面
1.安装Xrdp Windows远程桌面使用的是RDP协议,所以ubuntu上就要先安装Xrdp,终端命令行输入安装: sudo apt-get install xrdp vnc4server xba ...
- Keil的使用方法(汇总)
推荐 分享一个大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到人工智能的队伍中来! http://www.captainbed.net/strongerhuang 软件的开发 ...