java “无法找到 Spring NamespaceHandler”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3335203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"unable to locate Spring NamespaceHandler" error
提问by Marcelo Lacerda
I'm creating a stand-alone Sava application with Spring, to handle the JDBC access. The application works fine on every test and I decided that I need a jar to be deployed our clients.
我正在使用 Spring 创建一个独立的 Sava 应用程序,以处理 JDBC 访问。该应用程序在每次测试中都运行良好,我决定需要一个 jar 来部署我们的客户端。
They might not have spring in their classpath, so I used maven-assembly-plugin to handle the jar creation with dependencies.
他们的类路径中可能没有 spring,所以我使用 maven-assembly-plugin 来处理带有依赖项的 jar 创建。
However when I try to run the application:
但是,当我尝试运行该应用程序时:
java -jar target/myproject-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Which throws the following error:
这会引发以下错误:
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.
The applicationContext.xml file is in projectbase/src/main/resources. And its placed at target/packagename base.
applicationContext.xml 文件位于 projectbase/src/main/resources 中。并将其放置在目标/包名基础上。
The applicationContext.xml
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>
采纳答案by Marcelo Lacerda
I found the error, the bug lies in an unfixed bug in the maven-assembly plugin. I used the following workaround:
我发现了错误,错误在于maven-assembly 插件中的一个未修复的错误。我使用了以下解决方法:
First commented out the maven-assembly code in my pom. Then I copied the dependencies to a lib folder at the target using the maben-dependency-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>
Then I used the maven-jar-plugin to setup my executable jar:
然后我使用 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>
Finally I created a bash script that is deployed with the application that runs my app with its libs and any provided arguments:
最后,我创建了一个 bash 脚本,该脚本与应用程序一起部署,该应用程序使用其库和任何提供的参数运行我的应用程序:
java -cp lib/*:myproject-0.0.1-SNAPSHOT.jar org.foo.myproject.App $@
I should have built the app in python =/
我应该在 python 中构建应用程序 =/
回答by GrampaJohn
I ran into this problem today with the maven-assembly-plugin. A search brought me to this question, and a look a the bug report suggested that perhaps I was using the wrong plugin. So I switched to the maven-shade-plugin. It works perfectly, as far as I can tell. I have an executable jar that incorporates a number of Spring modules as well as Apache MQ. Here's the relevant bit from my pom.xml:
我今天用 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>
回答by skaffman
Looks like a bug in the Maven Assembly Plugin - MASSEMBLY-360, and as discussed in this blog entry here.
看起来像在Maven Assembly插件错误- MASSEMBLY-360,并在此博客条目讨论在这里。
In short, the meta-data files in the Spring JARs that handle the Spring namespaces are being mangled by maven.
简而言之,处理 Spring 命名空间的 Spring JAR 中的元数据文件正在被 maven 破坏。
回答by Vivek Gulati
onejar-maven-plugin lets you resolve maven and spring contradiction by creating a single jar file with dependencies. It creates a wrapper around your jar file by placing it inside another jar (one-jar).
onejar-maven-plugin 允许您通过创建具有依赖项的单个 jar 文件来解决 maven 和 spring 的矛盾。它通过将 jar 文件放置在另一个 jar(一个 jar)中来围绕您的 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>
回答by BajajG
In addition to the solution posted by @GrampaJohn, I also had to make sure that I was packaging the required libraries into the generated JAR file instead of extracting them into the generated JAR.
除了@GrampaJohn 发布的解决方案之外,我还必须确保将所需的库打包到生成的 JAR 文件中,而不是将它们解压缩到生成的 JAR 中。
In Eclipse Mars, after making changes to the pom.xml (added maven-shade plugin as suggested by @GrampaJohn), follow the steps:
在 Eclipse Mars 中,在对 pom.xml 进行更改后(按照@GrampaJohn 的建议添加了 maven-shade 插件),请按照以下步骤操作:
File -> Export -> Select Java folder -> Runnable JAR File-> enter the Launch configuration (main file), export destination, and in Library Handling, select "Package required Libraries into generated JAR-> Click finish
File -> Export -> Select Java folder -> Runnable JAR File-> 进入Launch配置(主文件),导出目的地,在Library Handling中选择“Package required Libraries into generated JAR->点击finish
.
.

