Spring源码解析--Spring配置文件解析NamespaceHandler(五)

上一篇博客 Spring源码解析--Spring配置文件解析BeanDefinitionParserDelegate(四)中我们介绍了对Bean基本元素的解析处理器,接下来我们介绍一下Spring提供的NamespaceHandler的处理机制,简单来说就是命名空间处理器,Spring为了开放性提供了NamespaceHandler机制,这样我们就可以根据需求自己来处理我们设置的标签元素。

Spring提供的NamespaceHandler处理器:


NamespaceHandler提供的接口方法:

public interface NamespaceHandler {


	void init();

	BeanDefinition parse(Element element, ParserContext parserContext);

	BeanDefinitionHolder decorate(Node source, BeanDefinitionHolder definition, ParserContext parserContext);

}
接下来我们介绍一下AopNamespaceHandler来对命名空间处理器做一个了解。

我们使用基于xml的spring配置时,可能需要配置如<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的命名空间才会找到AOP标签的处理器AopNamespaceHandler,在AOP的jar中的spring.handlers配置文件中配置了命名空间和命名空间处理器之间的关系。

内容:

http\://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler

在spring.schemas配置文件中添加了url和本地xsd标签解析文件的关系。

http\://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd
http\://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd
http\://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd
http\://www.springframework.org/schema/aop/spring-aop-3.1.xsd=org/springframework/aop/config/spring-aop-3.1.xsd
http\://www.springframework.org/schema/aop/spring-aop-3.2.xsd=org/springframework/aop/config/spring-aop-3.2.xsd
http\://www.springframework.org/schema/aop/spring-aop-4.0.xsd=org/springframework/aop/config/spring-aop-4.0.xsd
http\://www.springframework.org/schema/aop/spring-aop-4.1.xsd=org/springframework/aop/config/spring-aop-4.1.xsd
http\://www.springframework.org/schema/aop/spring-aop-4.2.xsd=org/springframework/aop/config/spring-aop-4.2.xsd
http\://www.springframework.org/schema/aop/spring-aop-4.3.xsd=org/springframework/aop/config/spring-aop-4.3.xsd
http\://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-4.3.xsd

在jar中已经内置了xsd文件,文件内容包含了命名空间处理器AopNamespaceHandler对于标签处理的规则。

对于上述内容的理解可以参考之前的文章 Spring源码学习--自定义标签

接下来我们介绍一下选择NamespaceHandler进行配置文件解析的流程。

在DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate)方法中会选择使用默认命名空间还是使用非默认命名空间进行处理。

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);
        }
    }
在BeanDefinitionParserDelegate的parseCustomElement中会进行选择NamespaceHandler进行配置文件解析处理。

public BeanDefinition parseCustomElement(Element ele) {
		return parseCustomElement(ele, null);
	}
	public BeanDefinition parseCustomElement(Element ele, BeanDefinition containingBd) {
		//获取xml配置文件中的命名空间http://www.springframework.org/schema/aop
		String namespaceUri = getNamespaceURI(ele);
		//根据命名空间找到命名空间处理类AopNamespaceHandler
		NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
		if (handler == null) {
			error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele);
			return null;
		}
		//解析命名空间支持的标签
		return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
	}

在AopNamespaceHandler中会看到其初始化定义的标签解析处理器。接下来进行的操作就是对标签进行解析处理最终生成BeanDefinition
public class AopNamespaceHandler extends NamespaceHandlerSupport {

	@Override
	public void init() {
		// In 2.0 XSD as well as in 2.1 XSD.
		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());
	}

}




### 回答1: "打包jar配置问题:无法找到Spring NamespaceHandler for的解决办法如下:" 在打包jar文件时,如果遇到了"configuration problem: unable to locate spring namespacehandler for"的错误,通常是因为缺少必要的Spring命名空间处理程序。 要解决此问题,您可以按照以下步骤进行操作: 1. 确保您的项目中已经包含了Spring的相关依赖。您可以通过在pom.xml文件中添加如下依赖来引入Spring: ``` <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>版本号</version> </dependency> ``` 2. 检查您的项目的类路径,确保所有必要的Spring依赖和配置文件都被正确添加。特别是,确保您的项目中存在Spring配置文件(如:applicationContext.xml)。 3. 确保您的项目中的配置文件中正确引入了Spring的命名空间。在Spring配置文件中,您需要使用`xmlns:context`或`xmlns:beans`等命名空间来引入相应的命名空间。 例如,在配置文件的根节点中添加以下代码来引入Spring的命名空间: ``` xmlns:context="http://www.springframework.org/schema/context" ``` 4. 检查您的项目的配置文件中是否存在语法错误或拼写错误。这些错误可能会导致Spring无法正确识别和解析命名空间。 5. 最后,如果您仍然遇到问题,可以尝试清除并重新构建您的项目,以确保所有的文件被正确编译和打包。 通过按照以上步骤检查和调整您的项目,您应该能够解决"configuration problem: unable to locate spring namespacehandler for"的错误,并成功打包您的jar文件。 ### 回答2: "打包jar配置问题:无法找到Spring NamespaceHandler for的解决方法是: 这个问题通常发生在使用Spring框架时,配置文件中引用了某些Springnamespace,但在打包成jar文件后无法正确定位到相应的处理程序。 要解决这个问题,可以按照以下步骤进行操作: 1. 确保项目的依赖项正确配置,并且所有的Spring相关库已经正确引入。可以通过检查项目的pom.xml文件或者gradle.build文件来确认依赖项是否正确。 2. 检查项目的配置文件,确保正确引入了Spring的命名空间和模式。在配置文件中添加如下代码: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 3. 确保配置文件中引用的schema和namespace的版本与项目中所使用的Spring版本相匹配。可以通过查看Spring官方文档来确定正确的版本。 4. 如果还是无法解决问题,可以尝试重新生成项目的IDE配置文件,或者是使用其他的集成开发环境重新导入项目。 通过以上步骤,应该能够解决打包jar时无法定位Spring NamespaceHandler for的问题。如果问题仍然存在,可能是其他配置或依赖项的问题,可以进一步排查和调试。 ### 回答3: 在使用Java打包jar时,如果出现了"configuration problem: unable to locate spring namespacehandler"的错误,意味着无法找到Spring的命名空间处理器。这可能是由于以下原因导致的: 1. 缺少必要的Spring依赖:首先,您需要确保在项目的构建路径中包含了所有必要的Spring依赖。可以通过使用Maven或Gradle等构建工具来管理您的依赖关系。确认您的构建工具配置文件(如pom.xml或build.gradle)中已正确添加Spring的依赖项,并重新构建项目。 2. 不正确的命名空间处理器配置:如果项目的Spring配置文件中包含了不正确的命名空间处理器配置,也会导致此问题。在您的Spring配置文件中,确保已正确配置了所需的命名空间处理器。例如,如果您使用了Spring MVC,则应在配置文件中添加以下命名空间处理器声明: ```xml xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" ``` 请注意,命名空间处理器的声明应与所使用的Spring版本和相应的XSD文件相匹配。 3. 不正确的类路径配置:另一个可能的原因是项目的类路径配置不正确,导致无法找到所需的Spring命名空间处理器类。在打包为jar文件时,确保您的构建工具已正确配置了类路径,以便包含所有必要的类和配置文件。 总之,当出现"configuration problem: unable to locate spring namespacehandler"的错误时,您应该检查项目的依赖关系、Spring配置文件和类路径配置,以确保正确引入了Spring的命名空间处理器,并确保项目能够正常运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值