博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring自定义标签的原理
阅读量:4216 次
发布时间:2019-05-26

本文共 3670 字,大约阅读时间需要 12 分钟。

转自:

Spring自定义标签的原理

XML通常通过DTD、XSD定义,但DTD的表达能力较弱,XSD定义则能力比较强,能够定义类型,出现次数等。自定义标签需要XSD支持,在实现时使用Namespace扩展来支持自定义标签。

当你在苦逼的写下面的代码时:

Xml代码  
  1. <bean id="beanId" class="com.xxx.xxxx.Xxxxx">  
  2.         <property name="property1">  
  3.             <value>XXXX</value>  
  4.         </property>  
  5.         <property name="property2">  
  6.             <value>XXXX</value>  
  7.         </property>  
  8.     </bean>  

 是不是会羡慕这样写代码呢?

Xml代码  
  1. <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:

 

Xml代码  
  1. http\://test.hatter.me/schema/test=me.hatter.test.TestNamespaceHandler  

 spring.schemas:

 

Xml代码  
  1. http\://test.hatter.me/schema/test/test.xsd=META-INF/test.xsd  

 test.xsd:

 

Xml代码  
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>  
  2. <xsd:schema xmlns="http://test.hatter.me/schema/test"  
  3.         xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
  4.         targetNamespace="http://test.hatter.me/schema/test">  
  5.   
  6.         <xsd:element name="custom" type="customType">  
  7.         </xsd:element>  
  8.           
  9.         <xsd:complexType name="customType">  
  10.                 <xsd:attribute name="id" type="xsd:ID">  
  11.                 </xsd:attribute>  
  12.                 <xsd:attribute name="name" type="xsd:string">  
  13.                 </xsd:attribute>  
  14.         </xsd:complexType>  
  15.   
  16. </xsd:schema>  

 me.hatter.test.TestNamespaceHandler:

 

Java代码  
  1. package me.hatter.test;  
  2.   
  3. import org.springframework.beans.factory.xml.NamespaceHandlerSupport;  
  4.   
  5. public class TestNamespaceHandler extends NamespaceHandlerSupport {  
  6.   
  7.     public void init() {  
  8.         registerBeanDefinitionParser("custom"new TestCustomBeanDefinitionParser());  
  9.     }  
  10. }  

 me.hatter.test.TestCustomBeanDefinitionParser:

 

Java代码  
  1. package me.hatter.test;  
  2.   
  3. import me.hatter.test.bean.TestBean;  
  4.   
  5. import org.springframework.beans.factory.config.BeanDefinition;  
  6. import org.springframework.beans.factory.support.RootBeanDefinition;  
  7. import org.springframework.beans.factory.xml.BeanDefinitionParser;  
  8. import org.springframework.beans.factory.xml.ParserContext;  
  9. import org.w3c.dom.Element;  
  10.   
  11. public class TestCustomBeanDefinitionParser implements BeanDefinitionParser {  
  12.   
  13.     public BeanDefinition parse(Element element, ParserContext parserContext) {  
  14.   
  15.         String id = element.getAttribute("id");  
  16.         String name = element.getAttribute("name");  
  17.   
  18.         RootBeanDefinition beanDefinition = new RootBeanDefinition();  
  19.         beanDefinition.setBeanClass(TestBean.class);  
  20.         beanDefinition.getPropertyValues().addPropertyValue("name", name);  
  21.         parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);  
  22.   
  23.         return beanDefinition;  
  24.     }  
  25. }  

 

测试代码

test.xml:

 

Xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:test="http://test.hatter.me/schema/test"  
  5.     xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.         http://test.hatter.me/schema/test http://test.hatter.me/schema/test/test.xsd">  
  8.           
  9.         <test:custom id="testCustom" name="this is a test custom tag" />  
  10. </beans>  

 me.hatter.test.main.Main:

Java代码  
  1. package me.hatter.test.main;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class Main {  
  7.   
  8.     public static void main(String[] args) {  
  9.         String xml = "classpath:me/hatter/test/main/test.xml";  
  10.         ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { xml });  
  11.         System.out.println(context.getBean("testCustom"));  
  12.     }  
  13. }  

 上例输出为:

TestBean[name=thisis a test custom tag]

转载地址:http://mlnmi.baihongyu.com/

你可能感兴趣的文章
[图文] Seata AT 模式分布式事务源码分析
查看>>
pm 源码分析
查看>>
Sending the User to Another App
查看>>
kmsg_dump
查看>>
Getting a Result from an Activity
查看>>
Allowing Other Apps to Start Your Activity
查看>>
dev/mem
查看>>
pfn_valid 源码分析
查看>>
dev/kmem 和dev/mem的区别
查看>>
checkbox
查看>>
Sending Simple Data to Other Apps
查看>>
Receiving Simple Data from Other Apps
查看>>
中断API之__tasklet_schedule
查看>>
中断API之enable_irq
查看>>
中断API之disable_irq
查看>>
nova 中的guestfs
查看>>
nova中的localfs
查看>>
utils/rpm_build.sh
查看>>
查看模块参数
查看>>
udev重命名网口
查看>>