简体   繁体   中英

Why spring cannot find beans.xml?

I am learning spring and put the Beans.xml into main/resources folder. I am using maven to manage project.

However when running, it cannot find the Beans.xml

 ApplicationContext context = new ClassPathXmlApplicationContext(
                "Beans.xml");

The error is Exception in thread "main"

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [Beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [Beans.xml] cannot be opened because it does not exist.

I am using Intellij, want to know why? I am using spring 4.3.4

new ClassPathXmlApplicationContext("file:src/main/resources/beans.xml");

file:前缀指向文件系统资源,而不是类路径。

Despite the @Hiccup's answer is right, but it is not accurate because if your application is FileSystem dependent (using the "file:" prefix) and you package this app and execute it with java commands there will be thrown the same exception, so you can add this prefix:

ApplicationContext context = new ClassPathXmlApplicationContext(
            "classpath:Beans.xml");

But this is correct just if you have configured Maven (or Ant or Ivy) to tell IntelliJ to locate resources from main/resources folder.

So, if the previous affirmation is not your case, you have to move your Beans.xml to src/main/java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM