Spring自定义标签的原理
XML通常通过DTD、XSD定义,但DTD的表达能力较弱,XSD定义则能力比较强,能够定义类型,出现次数等。自定义标签需要XSD支持,在实现时使用Namespace扩展来支持自定义标签。
当你在苦逼的写下面的代码时:
<bean id="beanId" class="com.xxx.xxxx.Xxxxx"> <property name="property1"> <value>XXXX</value> </property> <property name="property2"> <value>XXXX</value> </property> </bean>
是不是会羡慕这样写代码呢?
<xxx:xxxx id="beanId"/>
Spring通过XML解析程序将其解析为DOM树,通过NamespaceHandler指定对应的Namespace的BeanDefinitionParser将其转换成BeanDefinition。再通过Spring自身的功能对BeanDefinition实例化对象。
在期间,Spring还会加载两项资料:
-
META-INF/spring.handlers
指定NamespaceHandler(实现org.springframework.beans.factory.xml.NamespaceHandler)接口,或使用org.springframework.beans.factory.xml.NamespaceHandlerSupport的子类。 -
META-INF/spring.schemas
在解析XML文件时将XSD重定向到本地文件,避免在解析XML文件时需要上网下载XSD文件。通过现实org.xml.sax.EntityResolver接口来实现该功能。
制作自定义的标签
spring.handlers:
http\://test.hatter.me/schema/test=me.hatter.test.TestNamespaceHandler
spring.schemas:
http\://test.hatter.me/schema/test/test.xsd=META-INF/test.xsd
test.xsd:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <xsd:schema xmlns="http://test.hatter.me/schema/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test.hatter.me/schema/test"> <xsd:element name="custom" type="customType"> </xsd:element> <xsd:complexType name="customType"> <xsd:attribute name="id" type="xsd:ID"> </xsd:attribute> <xsd:attribute name="name" type="xsd:string"> </xsd:attribute> </xsd:complexType> </xsd:schema>
me.hatter.test.TestNamespaceHandler:
package me.hatter.test; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; public class TestNamespaceHandler extends NamespaceHandlerSupport { public void init() { registerBeanDefinitionParser("custom", new TestCustomBeanDefinitionParser()); } }
me.hatter.test.TestCustomBeanDefinitionParser:
package me.hatter.test; import me.hatter.test.bean.TestBean; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.BeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.w3c.dom.Element; public class TestCustomBeanDefinitionParser implements BeanDefinitionParser { public BeanDefinition parse(Element element, ParserContext parserContext) { String id = element.getAttribute("id"); String name = element.getAttribute("name"); RootBeanDefinition beanDefinition = new RootBeanDefinition(); beanDefinition.setBeanClass(TestBean.class); beanDefinition.getPropertyValues().addPropertyValue("name", name); parserContext.getRegistry().registerBeanDefinition(id, beanDefinition); return beanDefinition; } }
测试代码
test.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:test="http://test.hatter.me/schema/test" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://test.hatter.me/schema/test http://test.hatter.me/schema/test/test.xsd"> <test:custom id="testCustom" name="this is a test custom tag" /> </beans>
me.hatter.test.main.Main:
package me.hatter.test.main; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { String xml = "classpath:me/hatter/test/main/test.xml"; ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { xml }); System.out.println(context.getBean("testCustom")); } }
上例输出为:
TestBean[name=thisis a test custom tag]
相关推荐
spring的自定义标签代码工程样例,Chapter2工程
Spring 5.2.9 06 源码分析-spring自定义标签解析过程
Spring 5.2.9 06 源码分析-spring自定义标签解析过程
主要介绍了这一次搞懂Spring自定义标签以及注解解析原理说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
这一次搞懂Spring自定义标签以及注解解析原理说明.docx
扩展Spring, 自定义标签的实现, 包括 schema配置和自定义标签以及handler的加载过程
NULL 博文链接:https://wangxinchun.iteye.com/blog/2301263
用于自定义spring xml标签, 例如 <dubbo:service /> 标签
spring mvc freemarker 自定义标签demo spring mvc 通过自定义的标签在jsp页面调用,初学,记录下做个备忘
Spring框架是现在Java最流行的开源框架之一,需要实现一些自定义的标签,主要是方便使用我们框架的人能够快速、简单进行配置,有兴趣的可以了解一下。
实现spring自定义扩展标签的实现步骤
用springMVC+velocity时,当使用自定义标签,发现自定义标签无法注入spring的其它服务类,归根源码发现,velocity的源码中有一段,有一段代码永远获取的是新实例。所以,把这个地方的源码改了,所以使用这个的时候,...
Spring5.0自定义配置文件便签[Maven]工程可运行【spring-customize-tag】
主要给大家介绍了关于Spring源码解密之自定义标签与解析的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参借鉴,下面随着小编来一起学习学习吧。
spring自定义标签解析过程
jsp自定义标签,比如说if,foreach,out,format
spring 自定义标签技术 xml扩展例子,根据网上的文档整理的
结合spring,自定义xml标签,并利用factorybean来代理获取到的标签内容
spring自定义标签解析断点环境