我可以使用Maven编译和启动我的Spring项目:
mvn -e clean compile exec:java -Dexec.mainClass=de.fraunhofer.fkie.tet.vmware.manager.Test
但是,当我使用maven-assembly-plugin
(包括applicationContext.xml
)在一个文件中汇集所有jar时,我总是在Exception
执行期间获得java
:
java -cp target/test-jar-with-dependencies.jar:. de.fraunhofer.fkie.tet.vmware.manager.Test
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Sep 6, 2010 10:37:21 AM org.springframework.util.xml.SimpleSaxErrorHandler warning
WARNING: Ignored XML validation warning
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/context/spring-context.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
...
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
Line 10 in XML document from class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c:
The matching wildcard is strict, but no declaration can be found for element 'context:annotation-config'.
...
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c:
The matching wildcard is strict, but no declaration can be found for element 'context:annotation-config'.
我还尝试将模式定义(即spring-context.xsd
等)直接附加到类路径,但没有任何成功。
less src/main/resources/applicationContext.xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config /> <!-- wegen '@PostConstruct' -->
<!--<context:component-scan base-package="de.fraunhofer" /> -->
...
</beans>
答案 0 :(得分:19)
使用文件/META-INF/spring.schemas
和/META-INF/spring.handlers
解析Spring命名空间处理程序。因为具有这些名称的文件存在于不同的Spring jar中,所以在maven-assembly-plugin
之后可能只有其中一个保留在目标jar中。
也许您可以手动合并这些文件,并以某种方式配置maven-assembly-plugin
以使用此合并文件覆盖目标jar中的文件。
答案 1 :(得分:5)
我怀疑你的spring配置文件缺少context
XML命名空间。它应该添加到spring配置文件的根元素中,如下所示:
<?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"
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">
<context:annotation-config />
<context:component-scan base-package="com.abc.xyz" />
</beans>
答案 2 :(得分:3)
根据axtavt的回复,我发现了根本问题,我已将其报告为?bug?在Spring中:https://jira.springsource.org/browse/SPR-8368 - 包含生成这些文件的合并副本的解决方法。对于后代,代码也在这里:
//IOUtils and FileUtils come from Apache Commons IO
for(String s : new String[] {"spring.schemas", "spring.handlers", "spring.tooling"}) {
Enumeration<?> e = Test.class.getClassLoader().getResources("META-INF/"+s);
StringBuilder out = new StringBuilder();
while(e.hasMoreElements()) {
URL u = (URL) e.nextElement();
out.append(IOUtils.toString(u.openStream())).append("\n");
}
File outf = new File(s);
FileUtils.writeStringToFile(outf, out.toString(), "UTF-8");
}
答案 3 :(得分:1)
我相信这个问题有3个解决方案
答案 4 :(得分:0)
你的pom中有哪些弹簧依赖?由于某些spring jar文件不在类路径中,您可能会收到xml解析错误。在春季3,该库被分成许多jar文件。查看this post以了解您的需求,具体为:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
答案 5 :(得分:0)
因为它表明在Request或Response中解析存在问题。如果RestTemplate客户端期望来自资源的特定响应,但资源完全返回一些内容,则会发生此错误。我在POST RestTemplate客户端遇到此错误,该客户端与返回的响应中的更改有关。
例如返回实体'MyClass'的初始RestTemplate已更改并注视返回String,因此Parser开始提供错误
ResponseEntity<MyClass> postResponseEntity = restTemplate.postForEntity(postEndPoint, httpRequestEntity, MyClass.class);
MyClass postResponseBody = postResponseEntity.getBody();
已更改为
ResponseEntity<String> postResponseEntity = restTemplate.postForEntity(postEndPoint, httpRequestEntity, String.class);
String postResponseBody = postResponseEntity.getBody();
当响应类型从实体更改为String时,解析器在处理响应时开始出错。将其更改为正确的响应类型(在我的例子中是String)并且它开始工作。