问题描述:
org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 107; schema_reference.4: 无法读取方案文档 'http://www.springframework.org/schema/beans/springbeans-3.0.xsd', 原因为 1) 无法找到文档; 2) 无法读取文档; 3) 文档的根元素不是 <xsd:schema>。
出现这个问题的原因主要是两个方面:
1、spring xml配置文件中指定的xsd文件读取不到了,主要是因为断网或spring的官网暂时无法连接导致的;
2、可能是配置文件开头位置处的xmlns配置版本号不对。
解决方案:首先建议大家xml文档中的xsd文件使用无版本号的,我先给大家对比一下:
①有版本号的 'http://www.springframework.org/schema/beans/spring-beans-4.3.xsd'
因为用这个就要把Spring中的jar包的版本号和xsd文件的版本号进行比对,原则是jar包的版本号要大于等于xsd文件的,具体做法我没有亲自实践,工程量太大,有感兴趣的可以试一试。
②无版本号的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
这样就不用去关注版本号的问题了,同时也引出了我的下一步解决方案。
现在基本上都是采用maven来进行开发管理,需要把java工程打成可执行的jar包,必需把依赖的jar包也一起打包。而maven默认的package命令构建的jar包中只包括了工程自身的class文件,并没有包括依赖的jar包。可以通过配置maven-assembly-plugin插件来打包,pom具体配置如下:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<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>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.huawei.spring._01.App.MainClass</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
这个解决方法屡试不爽,以后也不用非要网络良好的情况下才能正常使用,而且也很方便,大家可以试一试。