前言: 自定义spring标签,类似dubbo服务在spring xml中注册。本文为如何自定义spring标签的教程。
step1:定义一个规范的bean
step2:在module的resources资源目录下新建目录META-INF(spring会自动从META-INF下读取配置文件),创建文件spring-custom.xsd(定义xsd标签)
targetNameSpace必须与xmlns保持一致
element表示定义标签,complexType表示bean属性
xsd标签定义规范传送门
step3:在module的resource资源新建META-INF文件夹(spring会自动从META-INF下读取配置文件),新建文件spring.schemas(名称不能变)
\反斜杠是转义,key为targetNameSpace+xsd文件名,value为xsd资源路径
step4:解析器parser解析不同bean标签
step5: handler中初始化注册bean的解析器
step6: 在module的resources资源新建META-INF,新建文件spring.handlers,注明该工程自定义的hander
key为targetNameSpace,value为自定义hander目录
step7: 测试用例以及目录
下篇文章,详细介绍spring bean自定义标签解析的源代码《spring如何识别各个namespace自定义标签》