Spring 自定义标签实现
参考资料:
dubbo对spring自定义标签的扩展的实现:https://my.oschina.net/lenglingx/blog/889662
一、自定义Spring标签简介
Spring官方文档 42.1中,介绍了如何自定义Spring标签,步骤如下:
1、编写一个XML Schema描述您的自定义元素
2、编写自定义命名空间处理程序实现,实现NamespackHandler接口
3、编写一个或多个自定义的Bean定义解析器,实现BeanDefinitionParser接口
4、注册到Spring容器
ps:官方文档描述出处,打开 https://docs.spring.io/spring/docs/4.3.20.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#extensible-xml-introduction ,搜索42.1即可
二、自定义标签实现
1、编写yf.xsd,名称根据自己的喜好来写即可
complexContent 该复杂类型只包含元素或不包含任何元素内容(空)
attribute 该复杂类型包含指定的属性。
annotation和documentation,结合起来,对定义的内容,做一定的文字说明
<xsd:annotation>
<xsd:documentation>
<![CDATA[ Namespace support for the yf services provided by yf framework. ]]></xsd:documentation>
</xsd:annotation>
1)定义一个type
<!-- xsd:complexType 定义一个type,及其属性attribute -->
<xsd:complexType name="applicationType">
<!-- 定义属性id, id值唯一 -->
<xsd:attribute name="id" type="xsd:ID">
<xsd:annotation>
<xsd:documentation><![CDATA[ The unique identifier for a bean. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
2)定义标签,并指定其引用哪个type,截取部分示例如下
<!-- 定义标签application,并指明其对应的type,示例 <yf:application></yf:application> -->
<xsd:element name="application" type="applicationType">
<xsd:annotation>
<xsd:documentation><![CDATA[ The application config ]]></xsd:documentation>
</xsd:annotation>
</xsd:element>
3)编写标签对应的实体类。(一个标签对应一个实体类)
两个标签:applaction、registry,实体类对应Application、Registry
2、编写标签解析类(因为命名空间中需要用到此类)
ApplicationBeanDefinitionParser.java
RegistryBeanDefinitionParser.java
3、编写命名空间处理类
public class YfNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
/**
* registerBeanDefinitionDecorator(String elementName, BeanDefinitionDecorator dec)
* 通过指定的BeandefiniParser解析xsd中定义的element元素
*/
registerBeanDefinitionParser("application", new ApplicationBeanDefinitionParser(Application.class, true));
registerBeanDefinitionParser("registry", new RegistryBeanDefinitionParser());
}
}
4、编写spring.handlers和spring.schemas串联起所有部件,二者都存放在META-INF文件夹下
spring.handlers指定NamespaceHandler,内容如下:
http\://yf.oschina.net/schema/yf= com.yf.springcustomtag.springsupport.YfNamespaceHandler
spring.schemas解析XML文件时将xsd文件重定向到本地文件,避免从网上下载xsd文件(通过实现org.xml.sax.EntityResolver接口来实现该功能),内容如下:
http\://yf.oschina.net/schema/yf.xsd=META-INF/yf.xsd
5、测试
1)编写spring配置applicationContext.xml,注意Beans中引入自定义标签的xmlns信息
xmlns:yf="http://yf.oschina.net/schema/yf"
http://yf.oschina.net/schema/yf http://yf.oschina.net/schema/yf.xsd
完整内容如下:
<?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:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:yf="http://yf.oschina.net/schema/yf"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://yf.oschina.net/schema/yf http://yf.oschina.net/schema/yf.xsd">
<!-- 上述Beans中须引用自定义标签的xsd信息 -->
<yf:application id="applicationTest" name="yfNameTest" version="1.0" />
<yf:registry id="yfRegistry" address="127.0.0.1" port="8080" />
</beans>
2)编写测试类
public class YfTagTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Application application1 = (Application) context.getBean("applicationTest");
System.out.println(application1.toString());
Registry registry1 = (Registry) context.getBean("yfRegistry");
System.out.println(registry1.toString());
}
}
3)运行测试类,结果如下,表明自定义标签测试成功。
23:34:29.734 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
23:34:29.743 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
23:34:29.752 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'applicationTest'
Application{id='applicationTest', name='yfNameTest', version='1.0'}
23:34:29.753 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'yfRegistry'
Registry{id='yfRegistry', address='127.0.0.1', port=8080}
Process finished with exit code 0
完整源码地址:https://gitee.com/yyhcsfy/LearningDemo/tree/master/spring-custom-tag