无法找到 Spring NamespaceHandler 实用程序

Unable to locate Spring NamespaceHandler util

当我使用 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文件。

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

一起提供

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

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

maven-assembly-plugin的问题:

您写道:

I also use the maven-assembly-plugin to create a executable jar file.

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

示例:spring.schema,共 spring-beans-3.1.1.RELEASE.jar

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

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

我猜你也会遇到 spring.handlers 文件的这个问题。

看来您可以配置 maven-assembly-plugin 来合并这些文件。参见:this answer of How 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-shade-plugin 类似的问题。 this solution.

涵盖了我的解决方案

基本上你必须在 pom.xml 中添加一个 AppendingTransformer 到你的 maven-shade-plugin 配置中。从下面添加 2 个 AppendingT运行sformer 条目:

<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>