【问题标题】:Unable to locate Spring NamespaceHandler util找不到 Spring NamespaceHandler util
【发布时间】:2018-05-18 02:23:25
【问题描述】:

当我使用 spring v.3.1 执行我的 java 项目时,出现以下错误:

Bean 'configParser'; nested exception is 

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/util]
Offending resource: class path resource [Services.xml]
Bean 'configParser'

我的 POM 有以下依赖:

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>

还有我的 Service.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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-3.1.xsd">

    <bean id="configParser" class="com.cc.mp.srv.core.parser.ConfigParser">
        <property name="file" >
            <util:constant static-field="com.cc.mp.srv.main.Main.FILE_NAME"/>
        </property>
    </bean>

为什么找不到实用程序架构?我在 pom 文件中插入核心依赖项。版本号也一样。 Spring 还需要什么?

编辑

Loading schema mappings from [META-INF/spring.schemas]
2015-01-31 18:56:11,553 DEBUG (main) [org.springframework.beans.factory.xml.PluggableSchemaResolver] - Loaded schema mappings: {http://www.springframework.org/schema/tx/spring-tx-2.5.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd, http://www.springframework.org/schema/tx/spring-tx-3.1.xsd=org/springframework/transaction/config/spring-tx-3.1.xsd, http://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd, http://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/config/spring-tx-3.1.xsd, http://www.springframework.org/schema/tx/spring-tx-3.0.xsd=org/springframework/transaction/config/spring-tx-3.0.xsd}

如果输出调试信息,则可以识别出使用了不同的版本号。可能是原因吗?如何定义数字?

我也使用 maven-assembly-plugin 创建一个可执行的 jar 文件。

【问题讨论】:

  • 您可以尝试从 xsd 导入中删除版本号吗?
  • 删除它们时出现相同的错误消息。

标签: java spring maven


【解决方案1】:

架构http://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/util/spring-util-3.1.xsd(以及带有版本的架构)随附spring-beans-&lt;version&gt;.RELEASE.jar

将此 jar 添加到您的依赖项中:

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-beans</artifactId>
     <version>3.1.2.RELEASE</version>
 </dependency>

ma​​ven-assembly-plugin 的问题:

你写道:

我也使用 maven-assembly-plugin 创建一个可执行的 jar 文件。

啊...我想 maven-assembly-plugin 是导致该问题的原因。这是因为模式解析机制以这种方式工作:Spring 提供带有 jar 的 XSD 文件。在 jar 中,文件夹 META-INF 中有一个文件 schema.info。此文件包含所有 XSD 文件的列表以及此 jar 提供的位置(在 jar 中)。

示例:spring-beans-3.1.1.RELEASE.jar 的 spring.schema

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-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd
http\://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.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-3.1.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd
http\://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.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-3.1.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd
http\://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd

所以现在你有不同的 jars (spring-beans, spring-tx, spring-aop, spring-context...) ,它们都包含一个 META-INF/spring.schemas 文件,具有不同的内容。另一方面,您使用maven-assembly-plugin 将所有jar 文件的内容聚合到一个文件中。

我猜spring.handlers 文件也会有这个问题。

看起来您可以配置maven-assembly-plugin 来合并这些文件。请参阅:this answerHow can I merge resource files in a Maven assembly?

另一种解决方案是使用spring-boot-maven-plugin 而不是maven-assembly-plugin

 <plugin>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-maven-plugin</artifactId>
     <version>1.2.1</version>
     <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>YOUR.MAIN.CLASS</mainClass>
    </configuration>
 </plugin>

(或使用maven-shade-pluging

【讨论】:

  • 我已添加更改,但出现相同的错误消息。
  • 我认为maven-assembly-plugin是关键
  • 谢谢,是的,问题出在了这里,所以我使用了 maven-shade-plugin。这是教程页面的链接crunchify.com/…
  • 谢谢。对我来说,它在使用 spring-boot-maven-plugin 而不是 maven-assembly-plugin 时有效
【解决方案2】:

我在使用 maven-shade-plugin 时遇到了类似的问题。 this solution 涵盖了我的解决方案。

基本上,您必须在 pom.xml 中将 AppendingTransformer 添加到您的 maven-shade-plugin 配置中。从下面添加 2 个 AppendingTransformer 条目:

<configuration>
    <outputDirectory>${basedir}/bin</outputDirectory>
    <finalName>myName</finalName>
    <transformers>
        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <mainClass>com.foo.MyApp</mainClass>
        </transformer>
        <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>

【讨论】:

    猜你喜欢
    • 2015-12-01
    • 2015-12-23
    • 2020-01-02
    • 2015-11-17
    • 2019-01-27
    • 1970-01-01
    • 2013-02-13
    • 2011-03-21
    • 2017-05-15
    相关资源
    最近更新 更多