自己构建一个Spring自定义标签以及原理讲解
平时不论是在Spring配置文件中引入其他中间件(比如dubbo),还是使用切面时,都会用到自定义标签。那么配置文件中的自定义标签是如何发挥作用的,或者说程序是如何通过你添加的自定义标签实现相应的功能的呢?且看下文。
通过对本文的阅读,你会在阅读涉及到自定义标签的源码功能时事半功倍,而且还可以自己动手做出一个自己的自定义标签。
先呈上我自己在本地实现自定义标签的代码及对应讲解:
1、先无脑输出一个测试要用到的Bean类
public class User {
private String userName;
private String emailAddress;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}
2、spring的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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:myname="http://www.zzq.com/schema/user"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.zzq.com/schema/user http://www.zzq.com/schema/user.xsd"> <aop:aspectj-autoproxy proxy-target-class="true"/>
<mvc:annotation-driven/>
<context:component-scan base-package="myDemoHome"/>
<context:property-placeholder location="classpath:properties/config.properties" ignore-unresolvable="true"/> <myname:myPw id="testUserBean" userName="zzq" emailAddress="zzqing@110.com"/> </beans>
3、从2中可以看到,命名空间中我添加了自定义的xmlns:myname="http://www.zzq.com/schema/user",以及http://www.zzq.com/schema/user跟http://www.zzq.com/schema/user.xsd。
其中紧跟xmlns冒号后面的部分,就是我们自定义标签引号前的部分,比如此处定义了myname,那么自定义标签中我就可以<myname:XXX/>这样引用了,其中的XXX则是在命名空间中定义的myPw。
中间http://www.zzq.com/schema/user对应此自定义标签的handler,放在Spring.handlers中。
最后的http://www.zzq.com/schema/user.xsd则定义了此自定义标签的XXX,即自定义标签冒号后面有什么,由此xsd定义,放在Spring.schemas中。
Spring.handlers跟Spring.schemas文件都放在META-INF目录下,因为spring会默认去此目录下读。
Spring.handlers如下所示:
http\://www.zzq.com/schema/user=myDemoHome.springElement.bdParser.UserNamespaceHandler
Spring.schemas如下所示:
http\://www.zzq.com/schema/user.xsd=META-INF/spring-test.xsd
3.1 自定义标签的解析类UserNamespaceHandler构建
package myDemoHome.springElement.bdParser; import myDemoHome.springElement.User;
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 UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { @Override
protected Class getBeanClass(Element element) {
return User.class;
} @Override
protected void doParse(Element element, BeanDefinitionBuilder builder) {
String name = element.getAttribute("userName");
String address = element.getAttribute("emailAddress"); if (StringUtils.hasText(name)) {
builder.addPropertyValue("userName", name);
}
if (StringUtils.hasText(address)) {
builder.addPropertyValue("emailAddress", address);
}
} }
就是对element中的标签进行解析处理,完成从xml中的标签属性向对象值的转化
3.2 自定义标签解析类的注册 UserNamespaceHandler
package myDemoHome.springElement.bdParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class UserNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("myPw", new UserBeanDefinitionParser());
}
}
此处的意思就是当遇到myPw这个标签的时候,往spring容器中注入这个标签的解析类,以完成后续对标签属性的解析。看到此处,各位道友有没有想起AOP的自定义注解aspectj-autoproxy 的解析呢?其实套路都是一样的。
3.3 xsd文件spring-test.xsd的定义
<?xml version="1.0" encoding="UTF-8" ?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.zzq.com/schema/user"
elementFormDefault="qualified"> <element name="myPw">
<complexType>
<attribute name="id" type="string"/>
<attribute name="userName" type="string"/>
<attribute name="emailAddress" type="string"/>
</complexType>
</element>
</schema>
此文件规定了自定义注解的标签,以及对应的属性
4、测试类ElementTest
package myDemoHome.springElement; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class ElementTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:spring/spring-config.xml");
User user = (User)applicationContext.getBean("testUserBean");
System.out.println(user.getEmailAddress());
}
}
再附上一张所用类的位置关系图

至此大功告成,最后运行一下测试类:

功德圆满!
总结:
构建一个自定义标签的流程便是如此,相信如果后面再遇到自定义标签,按照此构建思路反向解析一下便也能顺藤摸瓜知晓它的来龙去脉。其实看一下之前我们用过的中间件,像dubbo,也是一样的套路,只是功能更繁杂。
dubbo的jar包如图所示:

也是用了这三个实现的标签引用。
写在最后:至此,19年金三银四找工作之途的总结(包括技术跟个人感悟,感兴趣的道友可以移步我的另几篇博文一探究竟)便告一段落。今年自从去年下半年的资本收缩后,行情确实相较以往差了一些(虽然贫道也才17年入行),但我个人的感觉
是行业整体回归理性,之前是经过三四个月培训班下来就月薪十几K而且都抢着招人,太不正常。话说回来,不管外界行情怎样,只要你喜欢做这一行,都要静下心来多学习多探究,才能在不断的积累中让自己更上一层。今年年初两次阿里的面试
经历也让自己对于自身能力有了更客观真实的认识,不管是从项目经历还是个人技术积累上,都差很多。目前贫道新公司所处的部门,对技术比较注重,且技术大佬不少,工作都很认真,正是"发粪涂墙"之际,内心充满激情。后面的博文目前打算
更多偏向微服务(因为目前公司的项目用的就是微服务架构)、并发编程(因为前段时间买了本并发编程实战)。技术积累不是一朝一夕,与各位道友共勉!
自己构建一个Spring自定义标签以及原理讲解的更多相关文章
- 这一次搞懂Spring自定义标签以及注解解析原理
前言 在上一篇文章中分析了Spring是如何解析默认标签的,并封装为BeanDefinition注册到缓存中,这一篇就来看看对于像context这种自定义标签是如何解析的.同时我们常用的注解如:@Se ...
- spring基础---->spring自定义标签(一)
Spring具有一个基于架构的扩展机制,可以使用xml文件定义和配置bean.本博客将介绍如何编写自定义XML bean的解析器,并用实例来加以说明.其实我一直相信 等你出现的时候我就知道是你. Sp ...
- spring自定义标签之 自我实现
引言: 最近心情比较难以平静,周末的两天就跑出去散心了,西湖边上走走,看日落,还是不错的.回来博客上发现,在自定义标签上,最后一步实现忘记加上了.其实,人生的路程中,我们总是实现着自我的价值,让自己 ...
- spring自定义标签之 规范定义XSD
引言: spring的配置文件中,一切的标签都是spring定义好的.<bean/>等等,有了定义的规范,才能让用户填写的正常可用.想写自定义标签,但首先需要了解XML Schema De ...
- java_自定义标签运行原理
一.自定义标签运行原理: 二.文字说明 1.IE->web服务器 2.Web服务器->jsp 3.遇到自定义标签,首先实例化标签所对应的标签处理器类 4.调用setPageContext方 ...
- Spring 自定义标签配置
前景:经常使用一些依赖于Spring的组件时,发现可以通过自定义配置Spring的标签来实现插件的注入,例如数据库源的配置,Mybatis的配置等.那么这些Spring标签是如何自定义配置的?学习Sp ...
- Spring自定义标签
一.原理: 1.Spring通过XML解析程序将其解析为DOM树, 2.通过NamespaceHandler指定对应的Namespace的BeanDefinitionParser将其转换成BeanDe ...
- spring 自定义标签的实现
在我们进行Spring 框架开发中,估计用到最多的就是bean 标签吧,其实在Spring中像<mvc/><context/>这类标签以及在dubbo配置的标签都是属于自定义的 ...
- Spring自定义标签解析与实现
在Spring Bean注册解析(一)和Spring Bean注册解析(二)中我们讲到,Spring在解析xml文件中的标签的时候会区分当前的标签是四种基本标签(import.alias ...
随机推荐
- JS精度问题(0.1+0.2 = 0.3吗?)
一.引出问题 0.1+0.2 = 0.3吗?在JS中是这样的吗?我们写个测试代码不就知道了吗? 结果出人意料,并不像我们所想象的那样.那么这到底是为什么呢? 二.原因分析 JS浮点数存储机制: 三.解 ...
- 如何优化UI布局?
Android系统中填充布局是一个开销巨大的过程,每一个额外的嵌套布局和包含的View,都直接影响到应用程序的性能和响应能力.为了使应用程序流畅地运行和快速地响应,重要的是尽可能地保持布局的简单和避免 ...
- centos7搭建zabbix3.0监控系统
关闭防火墙和selinux systemctl stop firewalld.service (停止防火墙) systemctl disable firewalld.se ...
- 服务器http://localhost:8080要求用户输入用户名和密码
我们在将web项目部署运行的时候,想要在浏览器上输入http://localhost:8080时却提示: 如果你的电脑安装过Oracle的话,可能是和Oracle 的端口一样了,这是可以有两个办法解决 ...
- oracle 报错无法从套接字获取更多数据
报错信息如下: ---查看_optimizer_join_elimination_enabled参数值 切换sys用户 select a.ksppinm name, b.ksppstvl value, ...
- 从C过渡到C++的几个知识点(结构体、引用、重载运算符)
一.结构体和类(class) 下面一个使用结构体类型的例子 #include <iostream> using namespace std; struct Point{ // 声明Poin ...
- [LeetCode] Longest Mountain in Array 数组中最长的山
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length ...
- [LeetCode] Rectangle Overlap 矩形重叠
A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...
- python 用正则处理日志实例
前提: 了解正则基本语法 import re with open('top10_xiaozhuang_net.log','r') as f1: #读取日志文件 subject=f1.rea ...
- LinkedBlockingQueue 注记
近期看一个音频传输代码时,对方采用了LinkedBlockingQueue为生产者.消费者模式,来支撑读写线程. 个人感觉非常不错,因此也对这种方式进行总结,并梳理了一个基本的功能框架备用.主要两点: ...