spring自定义标签
2016-03-24 13:47
483 查看
Spring自定义标签的原理
XML通常通过DTD、XSD定义,但DTD的表达能力较弱,XSD定义则能力比较强,能够定义类型,出现次数等。自定义标签需要XSD支持,在实现时使用Namespace扩展来支持自定义标签。当你在苦逼的写下面的代码时:
Xml代码
<bean id="beanId" class="com.xxx.xxxx.Xxxxx">
<property name="property1">
<value>XXXX</value>
</property>
<property name="property2">
<value>XXXX</value>
</property>
</bean>
是不是会羡慕这样写代码呢?
Xml代码
<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代码
http\://test.hatter.me/schema/test=me.hatter.test.TestNamespaceHandler
spring.schemas:
Xml代码
http\://test.hatter.me/schema/test/test.xsd=META-INF/test.xsd
test.xsd:
Xml代码
<?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:
Java代码
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:
Java代码
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代码
<?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:
Java代码
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]
文章转载自:http://zhangxing119.iteye.com/blog/1796906
相关文章推荐
- Spring hibernate 声明式事务配置
- jdk与cglib的动态代理
- java 利用SMB读取远程文件
- Java中的Process类
- java中for和foreach的用法
- 来段代码提提神-Java实现文件压缩
- springmvc如何拦截上传文件最大限制异常
- Spring MVC 异常处理优解
- Java中的equals学习小结
- Java类加载与初始化机制实例分析
- JAVA程序元素在内存中的分配
- java反射机制方法的获取与调用
- java的一些发展前景!
- JHipster简介
- java的一些学习方案!
- java 学习资料
- Java核心知识点-Java多线程
- Spring4 版 eventbus 事件通信 事件驱动 介绍
- spark mllib 应用程序开发及提交到spark集群运行--入门
- 面向对象的java编程及面向对象的特征