spring读取applicationContext.xml,加载xsd错误

maven打包后启动程序遇到遇到解析spring的applicationContext.xml文件报错,错误如下:

org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring-beans-2.0.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xs


这是因为maven打包时,并没有将程序中spring jar包中的META-INF目录下的spring.schemas文件中的内容采用追加模式,而是采用覆盖策略。


可能你的maven pom.xml打包配置如下:

<plugin>

        <artifactId>maven-assembly-plugin</artifactId>

        <configuration>

          <descriptorRefs>

            <descriptorRef>jar-with-dependencies</descriptorRef>

          </descriptorRefs>

          <archive>

            <manifest>

              <mainClass></mainClass>

            </manifest>

          </archive>

        </configuration>

        <executions>

          <execution>

            <id>make-assembly</id>

            <phase>package</phase>

            <goals>

              <goal>single</goal>

            </goals>

           </execution>

        </executions>

      </plugin>

这是因为这样的配置采用的模式是文件覆盖,打包后spring-schema的内容丢失。

 

网上的许多帖子上给出的解决方案,但是并解决(有可能是自己mvn包的原因,shade的jar包下载下来的),程序无法识别:org.apache.maven.plugins.shade.resource.AppendingTransformer类。

maven-shade-plugin插件配置如下:

<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>
       </transformers>
      </configuration>
    </execution>
  </executions>
</plugin>

<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  <resource>META-INF/spring.schemas</resource>
</transformer>

 

 

最后,本人把<transformer>配置去掉,结果spring. schemas的内容采用了追加方式。

 

但是本人的项目依然没有运行成功,最后自己采用无依赖的jar,手动将依赖jar包复制到项目中,才解决了问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值