【问题标题】:org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException in spring compiled Jarorg.springframework.beans.factory.xml.XmlBeanDefinitionStoreException 在 spring 编译的 Jar 中
【发布时间】:2014-08-18 14:40:51
【问题描述】:

我环顾四周,但我还没有找到可以为我解决这个问题的解决方案。我有一个使用 Spring 的 Maven 项目,我调用了 assembly-single 并构建了一个可运行的 jar。该项目在 IDE 中运行良好,但是当我将其作为可运行 jar 运行时,出现以下异常:

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 8 in
 XML document from class path resource [properties.xml] is invalid; nested excep
tion is org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 62; cvc-elt.
1.a: Cannot find the declaration of element 'beans'.
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadB
eanDefinitions(XmlBeanDefinitionReader.java:396)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:334)
[...]

我的 properties.xml 文件如下所示。请注意,我的 schemaLocation 正确,第 8 行是 http://www.springframework.org/schema/context

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:util="http://www.springframework.org/schema/util"
    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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <context:property-placeholder
        location="classpath:test.properties"  system-properties-mode="OVERRIDE"/>

<!-- -->

</beans>

【问题讨论】:

  • 您使用的是哪个 Spring 版本?
  • 我相信 3.2.3.RELEASE

标签: java xml spring maven


【解决方案1】:

环顾其他解决方案,我看到有人建议将 xsd 的类路径直接放入 beans 标记中。所以我继续尝试这个。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
classpath:/org/springframework/beans/factory/xml/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
classpath:/org/springframework/context/config/spring-context-3.2.xsd
http://www.springframework.org/schema/util 
classpath:/org/springframework/beans/factory/xml/spring-util-3.2.xsd">
</beans>

此解决方案似乎适用于 bean,但不适用于上下文。我的下一个解决方案是我查看了我不久前发现的一篇文章,指出它可能与 Maven 覆盖 spring.schemas 文件而不是附加到文件有关 (this solution)。我意识到我的 spring.schemas 只包含 MVC 模式,所以我研究了使用 Maven Shade 构建我的 jar (using this as an example) 的建议。 Shade 将允许一个转换器告诉 maven 附加到文件而不是覆盖,从而允许多个依赖项使用同一个文件。

最后的pom:

<build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.mainClass</mainClass>
                        </manifest>
                    </archive>
                    <shadedArtifactAttached>true</shadedArtifactAttached>
                    <shadedClassifierName>jar-with-dependencies</shadedClassifierName>
                    <finalName>Filename</finalName>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
                <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>
                    </transformers>
                </configuration>

            </plugin>
        </plugins>

    </build>

【讨论】:

    【解决方案2】:

    当您处于离线模式时,我假设 Spring 使用其自己的 jar 中的模式来验证您的 bean。因此,从 spring 依赖版本开始,您应该为 xml 模式使用对齐的版本。

    在您的 properties.xml 描述符中,我可以看到您使用的是 3.0 架构版本,因此您应该使用 3.0.x 依赖版本,否则更新您的架构版本以匹配 Spring 版本。

    编辑:

    由于您使用的是 3.2 版本,请更新您的 xml bean 描述符以适应:

    <?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:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
            http://www.springframework.org/schema/util
            http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <!-- -->
    </beans>
    

    【讨论】:

    • 离线模式不是我确认的问题我自己的机器上也有这个问题。当我查看 spring-beans.jar 包时,我看到 .xsd 用于 3.0 格式与 3.0.x 格式。您的意思是更改 .xsd 引用吗?
    • 我确实最终自己使用了 3.2,但问题远不止于此。我认为 3.0 也可以正常工作,因为问题似乎与架构位置无关。请参阅我发布的答案。这是我的解决方案。
    猜你喜欢
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    相关资源
    最近更新 更多