Spring自定义标签和spring.handlers的加载过程
来源:互联网 发布:ios11怎么更新软件 编辑:程序博客网 时间:2024/06/06 04:08
原文链接:
Spring自定义标签:http://zhangxing119.iteye.com/blog/1796906
spring.handlers的加载过程:http://blog.csdn.net/kaka508/article/details/47258661
更全的介绍可以看spring 3.x
Spring自定义标签:
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.handlers的加载过程:
要实现自定义的xml配置,需要有两个默认spring配置文件来支持。一个是spring.schemas,一个是spring.handlers,前者是为了验证你自定义的xml配置文件是否符合你的格式要求,后者是告诉spring该如何来解析你自定义的配置文件。
1.在步骤4createReaderContext的时候,会做如下检查,如果没有resolver会创建一个默认的DefaultNamespaceHandlerResolver,
- 1
- 2
- 3
Spring.handlers这个文件名和路径就定义在这个类中。定义如下:
- 1
所以务必记住默认的文件路径是在META-INF文件夹下。
2.在步骤10中根据会根据传入的namespaceUri找到对应的NamespaceHandler,这个映射是在spring.handlers中配置的。
3在步骤13中会根据element的名字找到对应的BeanDefinitionParser,这个是在NamespaceHandler的init()方法里面来配置的。
- Spring自定义标签和spring.handlers的加载过程
- 读Spring的源代码七:spring.handlers的加载过程
- Spring的自定义标签
- Spring的加载过程
- spring中xml标签加载解析的一般过程
- Spring IOC源码分析-自定义标签加载
- Spring bean 标签加载、解析过程分析
- Spring bean 标签加载、解析过程分析
- Spring自定义标签的原理
- Spring自定义标签的原理
- Spring自定义标签的解析
- 解决MAVEN用assembly打包spring.handlers和spring.schemas出错的问题
- spring自定义配置文件的加载
- Web加载Spring的过程
- Web加载Spring的过程
- Spring Security的加载过程
- spring加载xsd的过程
- spring bean的加载过程
- Telnet功能打不开
- yum install update upgrade
- cat函数系统函数实现
- CI框架获取控制器名和方法名
- !--C语言解决实际应用题目--!
- Spring自定义标签和spring.handlers的加载过程
- vim的部分使用及makefile编写
- IdentityServer4(10)- 添加对外部认证的支持之QQ登录
- 安装 r matlab 2014a出现的问题
- C语言详解(1)数据类型
- Realm发布Realm .NET,扩展支持.NET技术栈
- 单峰函数
- C#事件
- html+css之margin使用方式