xml maven spring - 找不到元素“beans”的声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4972034/
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
maven spring - Cannot find the declaration of element 'beans'
提问by JJT
I have a spring project built with the usage of maven (packaged as jar) (dependencies are copied to a separate directory and added to classpath)
我有一个使用 maven 构建的 spring 项目(打包为 jar)(将依赖项复制到单独的目录并添加到类路径中)
I want to run it as java -jar
我想将它作为 java -jar 运行
The problem is that when I run it I get:
问题是,当我运行它时,我得到:
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
引起:org.xml.sax.SAXParseException:cvc-elt.1:找不到元素“beans”的声明。
It happens because spring.schemas and spring.handlers are located in a few jars i.e.: spring-beans, etc.
发生这种情况是因为 spring.schemas 和 spring.handlers 位于几个 jar 中,即:spring-beans 等。
Is there any way to solve it, supposing that I don't want to use shade plugin to unpack all dependencies and concatenate the content of spring.schemas and spring.handlers?
有没有什么办法可以解决,假设我不想使用shade插件来解包所有的依赖并将spring.schemas和spring.handlers的内容连接起来?
I would also like to avoid saving xsd files in my project and changing schemaLocation to point to my location.
我还想避免在我的项目中保存 xsd 文件并将 schemaLocation 更改为指向我的位置。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<camel:camelContext>
<camel:routeBuilder ref="fakeRouteBuilder"/>
</camel:camelContext>
<bean id="fakeRouteBuilder" class="<className>" />
</beans>
回答by rds
I know 2 situations where you can have this problem
我知道 2 种情况下您可能会遇到此问题
- If you don't have an internet connection (behind a proxy, for instance). In that case, download the XSD, copy it next to the XML, and change the schema location to
xsi:schemaLocation=" http://www.springframework.org/schema/beans spring-beans-2.5.xsd ..." - If you have an internet connection / still have the problem, that's because you use different versions of Spring for the XSD and the jar.
- 如果您没有互联网连接(例如在代理后面)。在这种情况下,下载 XSD,将其复制到 XML 旁边,然后将架构位置更改为
xsi:schemaLocation=" http://www.springframework.org/schema/beans spring-beans-2.5.xsd ..." - 如果您有互联网连接/仍然有问题,那是因为您为 XSD 和 jar 使用了不同版本的 Spring。
回答by Rajendra
I think the XSD would be part the spring jar, so shouldn't be issue.
我认为 XSD 将是弹簧罐的一部分,所以不应该是问题。
回答by Tomasz
Spring jars contain META-INF/spring.handlers and META-INF/spring.schemas files. To avoid overwriting the files when aggregating dependencies to one jar you can use maven Shade plugin:
Spring jar 包含 META-INF/spring.handlers 和 META-INF/spring.schemas 文件。为了避免在将依赖项聚合到一个 jar 时覆盖文件,您可以使用 maven Shade 插件:
回答by A. Masson
Got the same problem because I was behind a proxy. I was running a Spring MVC app under Tomcat 6. Just added the following lines at the biginning of my catalina.batfile and that solved my problem:
遇到了同样的问题,因为我在代理后面。我在 Tomcat 6 下运行 Spring MVC 应用程序。刚刚在我的catalina.bat文件的 biginning 添加了以下几行,这解决了我的问题:
set JAVA_OPTS=%JAVA_OPTS% -Dhttp.proxyHost=XXXX -Dhttp.proxyPort=YYYYY -Dhttp.proxyUser=ZZZZ -Dhttp.proxyPassword=XXXXXX
回答by Joffer
I was able to resolve a similar problem by clearing the xsd file from my remote resources cache, Window > Preferences > General > Network Connection > Cache
我能够通过从我的远程资源缓存中清除 xsd 文件来解决类似的问题,窗口 > 首选项 > 常规 > 网络连接 > 缓存
回答by ppeterka
META-INF/INDEX.LISTfile generated by the maven-assembly-plugin can also be a culprit.
META-INF/INDEX.LISTmaven-assembly-plugin 生成的文件也可能是罪魁祸首。
Cause: a Java bug: "ClassLoader.getResources() returns only 1 instance when using jar indexing"
原因:Java 错误:“ClassLoader.getResources() 在使用 jar 索引时仅返回 1 个实例”
Solution: disable the indexing in the assembly descriptor:
解决方案:禁用程序集描述符中的索引:
<indexed>false</indexed>
Resources: Spring JIRA SPR-5705JDK-6957241 ClassLoader.getResources() returns only 1 instance when using jar indexing
资源: Spring JIRA SPR-5705 JDK-6957241 ClassLoader.getResources() 使用 jar 索引时仅返回 1 个实例
回答by Sergio Trapiello
I had exactly the same problem and I think it is caused by 2 principal reasons:
我遇到了完全相同的问题,我认为这是由两个主要原因引起的:
- You have different versions for spring-security dependencies and XSD file references.
- Also it could happend if you use different versions of spring common framework and spring-security.
- 对于 spring-security 依赖项和 XSD 文件引用,您有不同的版本。
- 如果您使用不同版本的 spring 通用框架和 spring-security,也可能发生这种情况。
回答by zawhtut
I got that error when my XSD url pointing to different location that is different from XSD location defining in the jar file. For example, spring-beans-3.0.xsdinstead of spring-beans-2.5.xsdvise-versa.
当我的 XSD url 指向与 jar 文件中定义的 XSD 位置不同的不同位置时,我收到了该错误。例如,spring-beans-3.0.xsd而不是spring-beans-2.5.xsd反之亦然。
回答by Sean Patrick Floyd
There's a http://too much in your XSD declaration. Try this:
http://您的 XSD 声明中有太多内容。尝试这个:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
(yours was http://http://www.springframework.org/schema/beans/spring-beans-2.5.xsd)
(你的是http://http://www.springframework.org/schema/beans/spring-beans-2.5.xsd)

