java.io.FileNotFoundException:无法打开类路径资源,因为它不存在

时间:2017-03-17 17:07:40

标签: java spring file intellij-14

我正在尝试为我的项目设置配置位置,但我一直收到以下错误:

  

java.io.FileNotFoundException:类路径资源   无法打开[main / resources / app-context.xml],因为它没有   存在

我的项目设置如下:

enter image description here

我将代码设置为:

ApplicationContext context = new ClassPathXmlApplicationContext(configLocation: "main/resources/app-context.xml");

我该如何解决这个问题?

5 个答案:

答案 0 :(得分:16)

你直接放在src / main / java下的是默认包,位于类路径的根目录下。对于放在src / main / resources下的资源,它们是相同的:它们最终位于类路径的根目录。

因此资源的路径是app-context.xml,而不是main / resources / app-context.xml。

答案 1 :(得分:3)

我们也可以尝试此解决方案

ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:app-context.xml");

在这种情况下,spring会自动在类路径本身中找到该类

答案 2 :(得分:1)

试试这个:

ApplicationContext context = new ClassPathXmlApplicationContext("app-context.xml");

答案 3 :(得分:0)

文件位置/路径必须相对于类路径位置。如果资源目录在您的类路径中,则只需要“app-context.xml”作为文件位置。

答案 4 :(得分:0)

这对我有用 ApplicationContext context = new ClassPathXmlApplicationContext("app-context.xml");

相关问题