我环顾四周,所以没有找到可以为我解决此问题的解决方案。我有一个使用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>

最佳答案

环顾其他解决方案,我发现有人建议将xsd的类路径直接放入bean标记中。所以我继续尝试了。

<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)的建议。阴影将允许一个转换器,该转换器将告诉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>

关于java - Spring编译的Jar中的org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25365998/

10-10 17:13