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包複製到項目中,才解決了問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章