打成Jar包后運行報錯 Unable to locate Spring NamespaceHandler for XML schema namespace


MAVEN項目,在IDEA中運行正常,但是把它打成jar包后再運行就會出現異常:
 
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]
Offending resource: URL [jar:file:/home/jiashubing/mysoft/jartest3/cli3.jar!/META-INF/spring/spring-shell-plugin.xml]
 
這個錯誤並不少見,但是網上搜到的多為在項目運行的時候報這個錯誤。也有jar包中報這個錯誤的帖子,但是都說配置文件的版本不一致導致的。這些我都試過了,都無法解決。
 
嘗試了很多次以后,終於發現,雖然我在項目中是有spring-shell-plugin.xml這個配置文件的,但是 打成jar包以后,jar包中並沒有包含這個配置文件
 
原來,在IDEA中,MAVEN項目是會自動掃描 src/main/resources 路徑下的資源配置文件的,所以無需額外配置。
自動掃描的原因是:resource 下的文件編譯之后存放的位置,直接位於classes下面,這個路徑其實就是classPath的路徑,所以,在resources 根目錄下的配置文件其實就是 classPath的路徑
 
 
但是 打成Jar包以后,它是不會自動掃描 src/main/resources 路徑下的配置文件了,所以應當在這里手動配置,讓它能掃描到xml配置文件。
只需要在pom.xml中添加以下代碼:
<resources>
   <resource>
      <directory>src/main/resources</directory>
      <includes>
         <include>**/*.xml</include>
      </includes>
   </resource>
</resources>
 
原創文章,轉載請注明出處!

補充一下
MAVEN項目的標准目錄結構:
 
src
  -main
      –bin 腳本庫
      – java java源代碼文件
      – resources 資源庫,會自動復制到classes目錄里
      –filters 資源過濾文件
      –assembly 組件的描述配置(如何打包)
      –config 配置文件
      –webapp web應用的目錄。WEB-INF、css、js等
  -test
      – java 單元測試java源代碼文件
      – resources 測試需要用的資源庫
      –filters 測試資源過濾庫
  -site Site(一些文檔)
target
LICENSE.txt Project’s license
README.txt Project’s readme
 
紅色字體的四個目錄是常用的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM