![](/img/trans.png)
[英]Spring cannot find bean xml configuration file: “Failed to import bean definitions from relative location”
[英]I can´t import bean definitions from relative location
當我在applicationContext.xml
導入context-cfg.xml
時出現下一個錯誤。
GRAVE: Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem:
Failed to import bean definitions from URL location [classpath:/context-cfg.xml]
Offending resource: ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from
class path resource [context-cfg.xml]; nested exception is java.io.FileNotFoundException: class path
resource [context-cfg.xml] cannot be opened because it does not exist
應用上下文.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<import resource="classpath:/context-cfg.xml" /> //this line throws the error
</beans>
我的項目結構如下
誰能幫我?
來自Maven官方文檔: Maven項目結構
在工件生成源目錄(即 main 和 test)中,有一個用於 java 語言的目錄(在該目錄下存在正常的包層次結構),還有一個用於資源(根據默認資源定義復制到目標類路徑的結構) )。
所以你有兩個選擇:
選項1 :
將文件的位置更改為 Java 資源
選項2:
像這樣的更改語句<import resource="classpath:/resources/context-cfg.xml" />
在這種情況下,您的代碼將在 eclipse env 中正常工作,但是在您的 maven 構建期間,您需要將資源文件夾添加到您的類路徑中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.