您的位置:首页 > 编程语言 > Java开发

Spring Namespace扩展

2015-08-02 17:33 841 查看
在日常使用spring的时候,如果我们使用基于xml的spring配置,那么不可避免的需要配置许多节点,最常见的可能是<bean/>标签的配置,但除了这个之外,比如我们使用到aop的时候,可能需要配置如<aop:config />这样的标签节点,而在配置这个标签之前,通常我们需要引入这个aop标签所在的命名空间,如下面代码中红色加粗部分所示:

<?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">
这个命名空间的引入,既是限定我们所编写的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)方法,如下:

/**

* 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
BeanDefinition parseCustomElement(Element ele,BeanDefinition containingBd) {

   
String namespaceUri =ele.getNamespaceURI();

   
//这里根据节点对应的namespaceUri先获取改命名空间对应的NamespaceHandler

   
//1. this.readerContext.getNamespaceHandlerResolver()会获取到一个DefaultNamespaceHandlerResolver实例(在readerContext构造时赋值的)

   
//2. DefaultNamespaceHandlerResolver.resolve(String namespaceUri)根据传入uri查找得到对应handler返回,

   
//而具体的uri和Handler的配置配置信息是在META-INF/spring.handlers文件中配置的,这个文件可能会存在多个,但在加载之后会进行合并,

   
//如果我们想写自己的标签和自己的Handler那么我们也需要在META-INF/下放置spring.handlers这个文件,

   
//内容可参考spring-beans/META-INF/spring.handlers

   
NamespaceHandler handler =
this
.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);

   
if
(handler ==
null
) {

  
error(
"Unable to locate Spring NamespaceHandler for XML schema namespace ["
+ namespaceUri +
"]"
,ele);

  
return
null
;

   
}

   
//这里具体的handler负责相应的节点解析

   
return
handler.parse(ele,
new
ParserContext(
this
.readerContext,
this
,containingBd));

}


所有的NamespaceHandler都必须继承自org.springframework.beans.factory.xml.NamespaceHandlerSupport并实现init()方法,这个init方法中一般会向该handler中注册改命名空间下多个标签所对应的Paser处理类,如:

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());


}


}


spring自身除beans命名空间之外的命名空间解析过程用到了这一套实现逻辑,一些针对spring的扩展框架也用到了这一逻辑,典型的比如 阿里的dubbo

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: