九月 11, 2019 5:38:20 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6433a2: startup date [Wed Sep 11 17:38:20 CST 2019]; root of context hierarchy
九月 11, 2019 5:38:20 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [.idea/applicationContext.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [.idea/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist(AbstractXmlApplicationContext.java:93)
出現以上錯誤信息,最直接/根本原因酒是沒有找到applicationContext.xml配置文件;
出現此問題的根本原因是實例ApplicationContext時使用了以下方法來獲取配置文件:
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
但是,ClassPathXmlApplicationContext( ) 方法是在其所在的目錄中尋找 .xml 配置文件
注意: 這裏指的是編譯後的 .class 文件所在的目錄,不是 .java 文件
通常習慣 applicationContext.xml 文件在 src 目錄下,但是IDEA 默認的項目結構導致的(將 .java 文件和 .class 文件分開存放,.java文件存於 src 中,.class 文件存於 target 中),且idea默認是從target中讀取配置文件的,因此,ClassPathXmlApplicationContext( ) 方法無法找到 applicationContext.xml。
那如何解決呢?此時只需要將idea默認配置修改成src目錄即可。
注意:不能直接把 applicationContext.xml 移至 target 目錄下,因爲 .xml 配置文件運行時也需要在 .java 文件中獲取屬性信息
點擊 File -> Project Structure(或快捷鍵 Ctrl+Alt+Shift+S)
修改後,點擊apply->ok,重啓idea,重新運行即可。