簡體   English   中英

無法找到Spring NamespaceHandler util

[英]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文件中插入核心依賴項。 版本號也是一樣的。 什么需要春天呢?

編輯

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的問題:

你寫了:

我還使用maven-assembly-plugin來創建一個可執行的jar文件。

啊......我猜maven-assembly-plugin是造成這個問題的原因。 這是因為模式解析機制以這種方式工作:Spring提供帶有jar的XSD文件。 在jar中,文件夾META-INF是一個文件schema.info 此文件包含所有XSD fiels的列表以及此jar提供的位置(在jar中)。

例如:spring.sug of 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

所以現在你有不同的jar( spring-beansspring-txspring-aopspring-context ...),它們都包含一個META-INF/spring.schemas文件,內容不同。 另一方面,您使用maven-assembly-plugin在一個jar文件中聚合所有jar文件的內容。

我猜你也會spring.handlers文件的這個問題。

看起來您可以配置maven-assembly-plugin來合並這些文件。 請參閱: 這個答案 如何合並Maven程序集中的資源文件?

另一種解決方案是使用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類似的問題。 我的案例中的解決方案包含在此解決方案中

基本上你必須在pom.xml中的maven-shade-plugin配置中添加一個AppendingTransformer 從下面添加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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM