【问题标题】:"unable to locate Spring NamespaceHandler" error“无法找到 Spring NamespaceHandler”错误
【发布时间】:2011-03-21 02:19:03
【问题描述】:

我正在使用 Spring 创建一个独立的 Sava 应用程序,以处理 JDBC 访问。该应用程序在每次测试中都能正常运行,我决定需要一个 jar 来部署我们的客户端。

他们的类路径中可能没有 spring,所以我使用 maven-assembly-plugin 来处理带有依赖项的 jar 创建。

但是当我尝试运行应用程序时:

java -jar target/myproject-0.0.1-SNAPSHOT-jar-with-dependencies.jar

这会引发以下错误:

Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/p]
Offending resource: class path resource [applicationContext.xml]

at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
...and so on to the database access class of this project.

applicationContext.xml 文件位于 projectbase/src/main/resources 中。并将其放置在目标/包名基础上。

applicationContext.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:p="http://www.springframework.org/schema/p"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <bean id="dataSourceDesenv" class="org.apache.commons.dbcp.BasicDataSource"... />

    <bean id="simpleJdbcDaoSupport" class="org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport"
      p:dataSource-ref="dataSourceDesenv" />

    <bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
        <constructor-arg ref="dataSourceDesenv" />
    </bean>

</beans>

【问题讨论】:

  • 该错误意味着 applicationContext.xml 中缺少标头。请发布此文件,或至少发布标题和根元素以及一些示例。

标签: java spring maven-2


【解决方案1】:

我今天使用 maven-assembly-plugin 遇到了这个问题。搜索使我想到了这个问题,并且查看错误报告表明我可能使用了错误的插件。所以我切换到了 maven-shade-plugin。据我所知,它工作得很好。我有一个包含许多 Spring 模块以及 Apache MQ 的可执行 jar。这是我的 pom.xml 中的相关部分:

<build>
<finalName>sample-broker</finalName>
<plugins>
  ...
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.0</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>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>org.XYZ</mainClass>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>
</build>

【讨论】:

  • 感谢您的直接回答。这解决了我在使用 maven-assembly-plugin 时遇到的所有晦涩问题,而无需我浪费几天时间试图了解原因!
  • 这对我不起作用。 Maven 构建命令是否保持不变? mvn 全新安装?我试过这个以及 mvn clean package shade:shade。但我在两次尝试中都遇到了同样的问题。
  • 谢谢,我花了一夜的时间试图找出这个问题,它也对我有用
【解决方案2】:

我发现了错误,错误在于unfixed bug in the maven-assembly plugin。 我使用了以下解决方法:

首先在我的 pom.xml 中注释掉了 maven-assembly 代码。然后我使用 maben-dependency-plugin 将依赖项复制到目标的 lib 文件夹中:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

然后我使用 maven-jar-plugin 来设置我的可执行 jar:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
                <archive>
                    <index>true</index>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>org.foo.myproject.App</mainClass>
                    </manifest>
                    <manifestEntries>
                        <mode>development</mode>
                        <url>${pom.url}</url>
                        <key>value</key>

                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>

最后,我创建了一个 bash 脚本,该脚本与运行我的应用程序的应用程序一起部署,该应用程序带有它的库和任何提供的参数:

java -cp lib/*:myproject-0.0.1-SNAPSHOT.jar org.foo.myproject.App $@

我应该在 python 中构建应用程序 =/

【讨论】:

    【解决方案3】:

    看起来像是 Maven 程序集插件 - MASSEMBLY-360 中的一个错误,正如本博客条目 here 中所讨论的那样。

    简而言之,处理 Spring 命名空间的 Spring JAR 中的元数据文件正在被 maven 破坏。

    【讨论】:

      【解决方案4】:

      onejar-maven-plugin 允许您通过创建具有依赖关系的单个 jar 文件来解决 maven 和 spring 的矛盾。它通过将您的 jar 文件放在另一个 jar (one-jar) 中来创建一个包装器。

          <plugin>
                  <groupId>org.dstovall</groupId>
                  <artifactId>onejar-maven-plugin</artifactId>
                  <version>1.4.4</version>
                  <executions>
                    <execution>
                      <goals>
                          <goal>one-jar</goal>
                      </goals>
                    </execution>
                  </executions>
              </plugin>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-jar-plugin</artifactId>
                  <version>2.3.2</version>
                  <configuration>
                      <archive>
                          <manifest>
                              <mainClass>your.package.App</mainClass>
                          </manifest>
                      </archive>
                  </configuration>
              </plugin>
      
      <!-- outside build tag in pom.xml -->
      <pluginRepositories>
              <pluginRepository>
                  <id>onejar-maven-plugin.googlecode.com</id>
                  <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
              </pluginRepository>
      </pluginRepositories>
      

      【讨论】:

        【解决方案5】:

        除了@GrampaJohn 发布的解决方案之外,我还必须确保将所需的库打包到生成的 JAR 文件中,而不是将它们提取到生成的 JAR 中。

        在 Eclipse Mars 中,对 pom.xml 进行更改(按照@GrampaJohn 的建议添加了 maven-shade 插件),按照以下步骤操作:

        文件 -> 导出 -> 选择 Java 文件夹 -> 可运行的 JAR 文件 -> 输入 启动配置(主文件)、导出目标和 库处理,选择“将所需的库打包到生成的 JAR -> 点击完成

        .

        【讨论】:

          【解决方案6】:

          (假设您已经为 XML 添加了所有正确的标头,如果您仍然面临这个问题,这是我的情况) 我知道这个问题是用 Maven 提出的,但我使用的是 Gradle,我遇到了完全相同的问题。对于其他使用 Gradle 打包 Jars 并面临此问题的人,这是我的解决方法,

          jar {
              manifest {
                  attributes("Created-By"      : ".....",
                          "Specification-Title": ".....",
                          "Main-Class"         : "$mainClassName",
                          "Class-Path"         : configurations.compile.collect { it.getName() }.join(' '))
              }
          }
          

          添加“Class-Path”作为编译集合为我解决了这个问题。

          如果有人在使用上述代码后遇到此错误,

           Cannot change dependencies of configuration ':compile' after it has been resolved.
          

          您需要将此块移到“依赖项”块之后。

          【讨论】:

            猜你喜欢
            • 2013-02-13
            • 1970-01-01
            • 1970-01-01
            • 2015-10-29
            • 2017-05-15
            • 2016-08-10
            相关资源
            最近更新 更多