Java 无法为 XML 架构命名空间 http://www.springframework.org/schema/data/jpa 找到 Spring NamespaceHandler
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19615698/
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 for XML schema namespace http://www.springframework.org/schema/data/jpa
提问by Arry
I am using spring and hibernate in my java project which is managed by maven. I created an assembly (jar with dependencies) using following command mvn install assembly:assembly
我在由 maven 管理的 java 项目中使用 spring 和 hibernate。我使用以下命令创建了一个程序集(具有依赖项的 jar)mvn install assembly:assembly
Now, when I am trying to run my main class with the command: java -cp xyz-1.0-SNAPSHOT-jar-with-dependencies.jar com.xyz.class
then I am getting following error:
现在,当我尝试使用以下命令运行我的主类时:java -cp xyz-1.0-SNAPSHOT-jar-with-dependencies.jar com.xyz.class
然后我收到以下错误:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/data/jpa]**
Offending resource: class path resource [xyz-component-scans-config.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:76)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:271)
.
.
I am not understanding that why it is not able to find the NamespaceHandler? as I already have following dependencies in pom.
我不明白为什么它无法找到 NamespaceHandler?因为我在 pom.xml 中已经有了以下依赖项。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.1.0.RELEASE</version
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.0.2.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.0.RELEASE</version>
<scope>compile</scope>
</dependency>
I did try the suggestion in the following thread, but it didn't work for me. Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/data/jpa]
我确实尝试了以下线程中的建议,但它对我不起作用。 无法为 XML 架构命名空间找到 Spring NamespaceHandler [http://www.springframework.org/schema/data/jpa]
Source code for org.springframework.beans.factory.parsing.BeanDefinitionParsingException
org.springframework.beans.factory.parsing.BeanDefinitionParsingException 的源代码
回答by zmf
I used the [one-jar] plugin (https://code.google.com/p/onejar-maven-plugin/) for this.
为此,我使用了 [one-jar] 插件 ( https://code.google.com/p/onejar-maven-plugin/)。
I was having the same issue; namely maven assembly was messing up my spring.schema files. (Maven's plugin has been known to do this (spring forum link from another person experiencing the same issue)).
我遇到了同样的问题;即 maven 程序集弄乱了我的 spring.schema 文件。(Maven 的插件已经知道这样做(来自另一个遇到相同问题的人的 spring 论坛链接))。
If you really want to know what's going on here, expand your .jar file and look at the spring.schema and spring.handlers files. Look at the product of maven's assembly plugin, read this (Need understanding of spring.handlers and spring.schemas) stack overflow post that explains how those files are used.
如果您真的想知道这里发生了什么,请展开您的 .jar 文件并查看 spring.schema 和 spring.handlers 文件。查看 maven 的程序集插件的产品,阅读这篇(需要了解 spring.handlers 和 spring.schemas)堆栈溢出帖子,其中解释了如何使用这些文件。
回答by NickDK
I'm not really familiar with the maven assembly goal but the fact you've scoped these dependencies to compile time only could mean that these dependencies aren't included in the assembly. Could you try removing the:
我对 maven 程序集目标并不十分熟悉,但是您将这些依赖项的范围限定为编译时这一事实可能意味着这些依赖项未包含在程序集中。您可以尝试删除:
<scope>compile</scope>
<scope>compile</scope>
Parts from your dependency declarations?
部分依赖声明?
回答by axiopisty
You may be better off using the maven-shade-plugin to create your jar with dependencies. Here is an example of how to use the plugin:
您最好使用 maven-shade-plugin 来创建具有依赖项的 jar。以下是如何使用插件的示例:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>shade</goal></goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.stackexchange.stackoverflow.ExecutableJar</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>
</execution>
</executions>
</plugin>
In my experience the maven-shade-plugin is the best way to create an uber jar. See my other SO answerfor a more complete example. Note that this example doesn't require any 3rd party jars, but the maven-shade-plugin does handle them nicely. Give it a shot! :-)
根据我的经验,maven-shade-plugin 是创建 uber jar 的最佳方式。有关更完整的示例,请参阅我的其他SO 答案。请注意,此示例不需要任何 3rd 方 jar,但 maven-shade-plugin 确实可以很好地处理它们。试一试!:-)
回答by axiopisty
Can you make sure that you have Spring jar inside your WEB-INF/lib folder?
你能确保你的 WEB-INF/lib 文件夹中有 Spring jar 吗?
And also make sure that only 1 version exists.
并且还要确保只有 1 个版本存在。
If it still doesn't work, it will be helpful if you can attach the header of the configuration, in addition of the part of the body that you already pasted.
如果它仍然不起作用,如果您可以附加配置的标题以及您已经粘贴的正文部分,将会很有帮助。