[英]Context doesn't exist
春天語境我有一個非常奇怪的問題。
public static void main(String[] args) {
File file = new File("/home/user/IdeaProjects/Refactor/src/spring-cfg.xml");
System.out.println("Exist "+file.exists());
System.out.println("Path "+file.getAbsoluteFile());
ApplicationContext context = new ClassPathXmlApplicationContext(file.getAbsolutePath());
在控制台上顯示:
Exist true
Path /home/user/IdeaProjects/Refactor/src/spring-cfg.xml
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [home/user/IdeaProjects/Refactor/src/spring-cfg.xml]; nested exception is java.io.FileNotFoundException: class path resource [home/user/IdeaProjects/Refactor/src/spring-cfg.xml] cannot be opened because it does not exist
您正在嘗試加載它,好像/home/user/IdeaProjects/Refactor/src/spring-cfg.xml
是類路徑上的資源 - 它不是,它只是一個常規文件。 嘗試使用FileSystemXmlApplicationContext
代替...或指定一個真正的類路徑資源,例如spring-cfg.xml
假設你的src
目錄在你的類路徑中。
這不是很奇怪。 您正嘗試從不存在的文件中讀取上下文。
ClassPathXmlApplicationContext
,其名稱為true,不使用路徑作為絕對路徑,但它在類路徑中尋找。 你應該用
ApplicationContext context = new ClassPathXmlApplicationContext("/spring-cfg.xml");
注意:這將不是從src
讀取文件,而是從編譯的類中讀取文件(在編譯時它應該被復制到的位置)。
來自異常的消息是正確的,/ /home/user/IdeaProjects/Refactor/src/spring-cfg.xml
不是類路徑資源(看起來像是來自機器的常規路徑)。
我建議使用: ClassPathXmlApplicationContext("classpath:spring-cfg.xml")
因為你的config xml看起來像是在你的源文件夾中。
我認為這段代碼會起作用
ApplicationContext context =
new FileSystemXmlApplicationContext("file:/home/user/IdeaProjects/Refactor/src/spring-cfg.xml");
你可以在這里找到一些有用的信息http://static.springsource.org/spring/docs/2.5.6/reference/resources.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.