0%

Unable to locate Spring NamespaceHandler for XML schema namespace

使用“assembly”插件构建一个胖JAR包,加载该胖JAR包执行后,出现如下错误:

1
Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/p]

一般出现Unable to locate Spring NamespaceHandler for XML schema namespace错误的原因是缺少对相应Spring 包(比如“spring-core”)的依赖,但是这里经过确认,所有需要的Spring包都被正确依赖。
根据[1]可知,使用“assembly”插件构建胖JAR包时,目标胖JAR包中“META-INF”子目录下的“spring.schemas”和“spring.handlers”文件内容可能由于“互相覆盖”而缺失所需的Spring相关内容并最终导致出现Unable to locate Spring NamespaceHandler for XML schema namespace错误,验证我们这里所生成胖JAR包内上述两个文件的内容,发现果然缺失了所需的Spring相关内容。

“spring.schemas”文件内容如下:

1
http\://www.alibaba.com/schema/stat.xsd=META-INF/stat.xsd

“spring.handlers”文件内容如下:

1
http\://www.alibaba.com/schema/stat=com.alibaba.druid.support.spring.stat.config.DruidStatNamespaceHandler

接下来使用“shade”插件构建胖JAR包,且配置目标胖JAR包中“META-INF”子目录下“spring.schemas”和“spring.handlers”文件的生成策略是“合并”而非“互相覆盖”,具体插件配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
构建新胖JAR包,现在构建得胖JAR包中“META-INF”子目录下的“spring.schemas”和“spring.handlers”文件内容正确保留了所需的Spring相关内容,加载该胖JAR包执行不再出现`Unable to locate Spring NamespaceHandler for XML schema namespace`错误。

“spring.schemas”文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
http\://www.alibaba.com/schema/stat.xsd=META-INF/stat.xsd
http\://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd
http\://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http\://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http\://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd
http\://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd
http\://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd
http\://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd
http\://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd
http\://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd
http\://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd
http\://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd
...(篇幅所限,以上只是部分文件内容)

“spring.handlers”文件内容如下:

1
2
3
4
5
6
7
8
9
10
http\://www.alibaba.com/schema/stat=com.alibaba.druid.support.spring.stat.config.DruidStatNamespaceHandler
http\://www.springframework.org/schema/p=org.springframework.beans.factory.xml.SimplePropertyNamespaceHandler
http\://www.springframework.org/schema/util=org.springframework.beans.factory.xml.UtilNamespaceHandler

http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler
http\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler
http\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler
http\://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandler

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

**参考文献:** [1]https://stackoverflow.com/questions/31892587/spring-4-2-dependancy-injection-http-www-springframework-org-schema-p-erro
您的支持将鼓励我继续分享!