maven spring - 找不到元素“beans”的声明;

发布于 2024-10-17 12:34:52 字数 1059 浏览 1 评论 0原文

我有一个使用 maven 构建的 spring 项目(打包为 jar)(依赖项被复制到单独的目录并添加到类路径)

我想将其作为 java -jar 运行

问题是,当我运行它时,我得到:

Caused作者:org.xml.sax.SAXParseException:cvc-elt.1:找不到声明 元素“豆子”。

发生这种情况是因为 spring.schemas 和 spring.handlers 位于几个 jar 中,即:spring-beans 等。

假设我不想使用 shade 插件来解压所有依赖项并连接spring.schemas 和 spring.handlers 的内容?

我还想避免在项目中保存 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>

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)

I want to run it as 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'.

It happens because spring.schemas and spring.handlers are located in a few jars i.e.: spring-beans, etc.

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?

I would also like to avoid saving xsd files in my project and changing schemaLocation to point to my location.

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

硬不硬你别怂 2024-10-24 12:34:52

您可能会遇到此问题

  • 我知道在两种情况下,如果您没有互联网连接(例如在代理后面), 。在这种情况下,请下载 XSD,将其复制到 XML 旁边,然后将架构位置更改为 xsi:schemaLocation=" http://www.springframework.org/schema/beans spring-beans-2.5.xsd 。 .."
  • 如果您有互联网连接/仍然有问题,那是因为您对 XSD 和 jar 使用了不同版本的 Spring。

I know 2 situations where you can have this problem

  • 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.
热鲨 2024-10-24 12:34:52

我认为 XSD 将是 spring jar 的一部分,所以不应该成为问题。

I think the XSD would be part the spring jar, so shouldn't be issue.

jJeQQOZ5 2024-10-24 12:34:52

Spring jar 包含 META-INF/spring.handlers 和 META-INF/spring.schemas 文件。为了避免在将依赖项聚合到一个 jar 时覆盖文件,您可以使用 maven Shade 插件:

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:

http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#AppendingTransformer

森林迷了鹿 2024-10-24 12:34:52

遇到同样的问题,因为我在代理后面。我在 Tomcat 6 下运行 Spring MVC 应用程序。只需在我的 catalina.bat 文件的 biginning 中添加以下行即可解决我的问题:

set JAVA_OPTS=%JAVA_OPTS% -Dhttp.proxyHost=XXXX -Dhttp.proxyPort=YYYYY -Dhttp.proxyUser=ZZZZ -Dhttp.proxyPassword=XXXXXX

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.bat file and that solved my problem:

set JAVA_OPTS=%JAVA_OPTS% -Dhttp.proxyHost=XXXX -Dhttp.proxyPort=YYYYY -Dhttp.proxyUser=ZZZZ -Dhttp.proxyPassword=XXXXXX
や莫失莫忘 2024-10-24 12:34:52

我能够通过从远程资源缓存中清除 xsd 文件来解决类似的问题,Window >首选项>一般>网络连接>缓存

I was able to resolve a similar problem by clearing the xsd file from my remote resources cache, Window > Preferences > General > Network Connection > Cache

朱染 2024-10-24 12:34:52

maven- assembly-plugin 生成的 META-INF/INDEX.LIST 文件也可能是罪魁祸首。

原因:Java 错误:“ClassLoader.getResources() 在使用 jar 索引时仅返回 1 个实例”

解决方案:禁用程序集描述符中的索引:

<indexed>false</indexed>

资源:
Spring JIRA SPR-5705
JDK-6957241 ClassLoader.getResources() 在使用 jar 索引时仅返回 1 个实例

META-INF/INDEX.LIST file generated by the maven-assembly-plugin can also be a culprit.

Cause: a Java bug: "ClassLoader.getResources() returns only 1 instance when using jar indexing"

Solution: disable the indexing in the assembly descriptor:

<indexed>false</indexed>

Resources:
Spring JIRA SPR-5705
JDK-6957241 ClassLoader.getResources() returns only 1 instance when using jar indexing

各自安好 2024-10-24 12:34:52

您的 XSD 声明中的 http:// 过多。试试这个:(

<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">

你的是http://http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

There's a http:// too much in your XSD declaration. Try this:

<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)

凉宸 2024-10-24 12:34:52

当我的 XSD url 指向与 jar 文件中定义的 XSD 位置不同的不同位置时,我收到该错误。例如,spring-beans-3.0.xsd 而不是 spring-beans-2.5.xsd 反之亦然。

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.xsd instead of spring-beans-2.5.xsd vise-versa.

童话里做英雄 2024-10-24 12:34:52

您也可以使用 这里解决这个问题

Also you can use advice from here to solve this problem

梦境 2024-10-24 12:34:52

我遇到了完全相同的问题,我认为这是由两个主要原因引起的:

  • 您的 spring-security 依赖项和 XSD 文件引用有不同的版本。
  • 如果您使用不同版本的 spring 公共框架和 spring-security,也可能会发生这种情况。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文