class path resource [spring/] cannot be resolved to URL because it does not exist問題解決,轉


轉自:https://blog.csdn.net/hanxueyu666/article/details/72919406

maven創建web工程的時候報下面的錯誤

org.springframework.beans.factory.BeanDefinitionStoreException: Could not resolve bean definition resource pattern [classpath:spring/applicationContext-*.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring/] cannot be resolved to URL because it does not exist
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:229)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.io.FileNotFoundException: class path resource [spring/] cannot be resolved to URL because it does not exist
at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:187)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.findPathMatchingResources(PathMatchingResourcePatternResolver.java:412)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.getResources(PathMatchingResourcePatternResolver.java:286)
at org.springframework.context.support.AbstractApplicationContext.getResources(AbstractApplicationContext.java:1159)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
... 19 more

 

查閱資料有人提出如下原因(http://blog.csdn.net/moneyshi/article/details/53287036)

 

一、問題的原因

 

為何會出現這種FIleNotFoundException呢?  原因無非兩點:

1、路徑配置錯誤 

2、編譯有問題,文件不存在

 

二、解決辦法:

對於第一個原因,這個我們不多說,大家自己看看Java Web classpath詳解,改好配置目錄路徑,第一個原因排除,給大家一個案例參考

 

 

至於第二個原因,編譯不成功,導致文件不存在,拿Eclipse or MyEclipse為例,  首先去項目的編譯目錄查看資源文件和class文件是否編譯成功

如果編譯目錄的下不存在資源文件,那么編譯不成功, 解決辦法也很簡單,選擇Eclipse 工具欄上的Project , 選擇clean up ,並勾選下面的 Build Automatically,這樣就會自動編譯了,等編譯完成,你再看文件目錄,是否存在資源文件,存在了,則完美解決

 

如果還不行,那么看看你是否是通過第三方插件或工具來進行編譯的,重新對項目進行編譯就好了

 

通過對比發現我是第二種原因,但用上述的方法不能解決后來想到了,是下面的原因造成的,要在工程的pom中,添加如下的

<build>
<resources>
            <resource>

    <!-- 原文章這里是src/main/resource  少寫一個“s”被坑了T_T -->
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
</build>

如果不添加上面的語句,maven在編譯的時候就會打.xml和.properties最為后綴的文件過濾掉,所以就會報錯了

 


免責聲明!

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



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