gpt4 book ai didi

java - spring编译Jar中的org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException

转载 作者:行者123 更新时间:2023-11-30 09:04:06 24 4
gpt4 key购买 nike

我环顾四周,所以我还没有找到解决这个问题的解决方案。我有一个使用 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 的类路径直接放入 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>

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com