Spring编译的Jar中的org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException

时间:2014-08-18 14:40:51

标签: java xml spring maven

我环顾四周,所以我找不到能解决这个问题的解决方案。我有一个使用Spring的Maven项目,我调用assembly-single并构建一个可运行的jar。这个项目在IDE中工作正常但是当我将它作为runnable 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>

2 个答案:

答案 0 :(得分:2)

环顾其他解决方案我看到有些人建议将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>

答案 1 :(得分:0)

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

properties.xml 描述符中,我可以看到您正在使用 3.0 架构版本,因此您应该使用 < em> 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>