产生问题条件:
maven项目,其中项目使用到了mybatis,mybatis的配置文件xml包含了中文。
报错信息:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [spring-db.xml]
Offending resource: class path resource [spring/spring-config.xml]; nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 24 in XML document from class path resource [spring/spring-db.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 24; columnNumber: 20; Invalid byte 3 of 3-byte UTF-8 sequence.
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:118)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:103)
从错误上面看,就是xml文件编码问题,接着开始查看XML编码
1、发现确实不是UTF-8编码(这种情况可能拷贝被人的代码导致),然后把编码转换成UTF-8,接着你会发现,问题可能还在;
2、第一步后,就蒙了。。。
解决方案(把资源文件统一采用UTF-8编码):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>