java - 无法找到 Spring NamespaceHandler util

标签 java spring maven

当我使用 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.xsd , http://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>

ma​​ven-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-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

现在你有不同的 jar ( 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 )

关于java - 无法找到 Spring NamespaceHandler util,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28254417/

相关文章:

java - 没有每个单独文件的新扩展类

java - 如何将 AD 组映射到用户角色 Spring Security LDAP

java - ChromeDriver Selenium 出现 "java.lang.module.InvalidModuleDescriptorException: Provider class org.apache.bsf.BSFManager not in module"错误

java - Hibernate 使用分离对象状态覆盖数据库修改

java - 为什么我们需要在资源和授权服务器中完全相同的配置

java - Maven:缺少 Artifact org.springframework:spring:jar:4.2.6

java - 在 openjdk11 下执行 sonar-maven-plugin 时不支持的类文件主要版本 55

java - Spring 和 Web 服务

java - 使用 Maven 的 Spring Boot Web 应用程序不返回 HTML 页面

java - 在 war 中打包 javascript 应用程序(使用 grunt 和 browserify)