Spring Namespace扩展
2015-08-02 17:33
841 查看
在日常使用spring的时候,如果我们使用基于xml的spring配置,那么不可避免的需要配置许多节点,最常见的可能是<bean/>标签的配置,但除了这个之外,比如我们使用到aop的时候,可能需要配置如<aop:config />这样的标签节点,而在配置这个标签之前,通常我们需要引入这个aop标签所在的命名空间,如下面代码中红色加粗部分所示:
这个命名空间的引入,既是限定我们所编写的aop开头的标签必须符合spring-aop.xsd的定义限定,如 aop下各子节点元素定义的先后顺序等,在真正的容器启动加载的过程中,这些标签所定义的信息是如何解析并载入的呢?
spring在载入bean的时候,对各个子节点的载入主要分成了两类,一类是默认命名空间下元素节点的载入(即http://www.springframework.org/schema/beans空间之下的元素节点,主要是bean、import、alias等这几个最常用标签),另一类则是个性化命名空间下元素节点的载入(所谓的个性化命名空间指的是除了http://www.springframework.org/schema/beans空间之外的诸如http://www.springframework.org/schema/aop这类命名空间,当然也包括自定义命名空间);具体处理可参考
org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(Element root,BeanDefinitionParserDelegate delegate)方法,如下:
对于默认命名空间下的节点解析这里略过不讲,对于非默认空间下的节点解析作如下深入讨论:
这里首先明确几点:
spring对于非默认空间下标签的解析处理都是有对应的XXXNamespaceHandler的,比如解析<aop:xxx />标签,其有一个对应的叫做AopNamespaceHandler的Handler存在,所有在aop命名空间之下的几点的解析交给该Handler处理;这一点从如下代码可以窥得一斑:
所有的NamespaceHandler都必须继承自org.springframework.beans.factory.xml.NamespaceHandlerSupport并实现init()方法,这个init方法中一般会向该handler中注册改命名空间下多个标签所对应的Paser处理类,如:
spring自身除beans命名空间之外的命名空间解析过程用到了这一套实现逻辑,一些针对spring的扩展框架也用到了这一逻辑,典型的比如 阿里的dubbo
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> |
spring在载入bean的时候,对各个子节点的载入主要分成了两类,一类是默认命名空间下元素节点的载入(即http://www.springframework.org/schema/beans空间之下的元素节点,主要是bean、import、alias等这几个最常用标签),另一类则是个性化命名空间下元素节点的载入(所谓的个性化命名空间指的是除了http://www.springframework.org/schema/beans空间之外的诸如http://www.springframework.org/schema/aop这类命名空间,当然也包括自定义命名空间);具体处理可参考
org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(Element root,BeanDefinitionParserDelegate delegate)方法,如下:
/** * Parse the elements at the root level in the document: * "import","alias","bean". * @param root the DOM root element of the document */ protected void parseBeanDefinitions(Element root,BeanDefinitionParserDelegate delegate) { if (delegate.isDefaultNamespace(root.getNamespaceURI())) { NodeList nl = root.getChildNodes(); for ( int i = 0 ; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element) { Element ele = (Element) node; String namespaceUri =ele.getNamespaceURI(); if (delegate.isDefaultNamespace(namespaceUri)) { //这里讲将对默认命名空间(http://www.springframework.org/schema/beans)下的标签节点(bean、import、alias等)进行处理 parseDefaultElement(ele,delegate); } else { //这里对非默认命名空间下的标签进行处理 delegate.parseCustomElement(ele); } } } } else { delegate.parseCustomElement(root); } } |
这里首先明确几点:
spring对于非默认空间下标签的解析处理都是有对应的XXXNamespaceHandler的,比如解析<aop:xxx />标签,其有一个对应的叫做AopNamespaceHandler的Handler存在,所有在aop命名空间之下的几点的解析交给该Handler处理;这一点从如下代码可以窥得一斑:
|
public class AopNamespaceHandler extends NamespaceHandlerSupport { /** * Register the {@link BeanDefinitionParser BeanDefinitionParsers} for the * '<code>config</code>','<code>spring-configured</code>','<code>aspectj-autoproxy</code>' * and '<code>scoped-proxy</code>' tags. */ public void init() { //这里依次注册多个Parser处理类,他们的作用是分别处理<aop:config />、<aop:aspectj-autoproxy />标签,由此可见具体对每个标签的解析逻辑实际上是放在了Parser类(parse方法中) registerBeanDefinitionParser( "config" , new ConfigBeanDefinitionParser()); registerBeanDefinitionParser( "aspectj-autoproxy" , new AspectJAutoProxyBeanDefinitionParser()); registerBeanDefinitionDecorator( "scoped-proxy" , new ScopedProxyBeanDefinitionDecorator()); // Only in 2.0 XSD: moved to context namespace as of 2.1 registerBeanDefinitionParser( "spring-configured" , new SpringConfiguredBeanDefinitionParser()); } } |
相关文章推荐
- MyEclipse的导出war包问题
- 文件的上传与下载
- [leetcode-47]Permutations II(java)
- 重学java23种设计模式(8)组合模式
- [leetcode-46]Permutations(java)
- java程序设计之反弹高度
- Eclipse(MyEclipse) 设置默认编码为Utf-8 之最全方法
- 什么是JAVABEAN?
- java验证码代码
- Java的基本数据类型
- Java常用类:String
- Java Performance Optimization
- java程序设计之完数
- 关于javaIDE开发环境JDK,java compiler的问题
- Eclipse(MyEclipse) 自动补全
- java中Scanner和标准输入流的哪一种更高效呢
- Eclipse之CTRL+左键直接进入方法函数Implementation
- new JFileChooser()遭遇AssertionError的解决方案
- Eclipse设置:背景与字体大小和xml文件中字体大小调整
- java反射机制