maven spring - 找不到元素'beans'的声明

JJT*_*JJT 13 xml schema spring maven-2 maven

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

我想以java -jar运行它

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

引起:org.xml.sax.SAXParseException:cvc-elt.1:找不到元素'beans'的声明.

之所以会发生这种情况,是因为spring.schemas和spring.handlers位于几个罐子中,即: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>
Run Code Online (Sandbox Code Playgroud)

rds*_*rds 24

我知道有两种情况可以解决这个问题

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


Raj*_*dra 8

我认为XSD将成为弹簧罐的一部分,所以不应该是问题.


小智 5

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

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


Sea*_*oyd 0

您的 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">
Run Code Online (Sandbox Code Playgroud)

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