Spring类路径资源[applicationContext.xml]不存在,无法打开

cbl*_*bll 2 java xml spring

我将放在applicationContext.xml与 Java 类相同的目录和包中。

执行以下操作来阅读它:

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Run Code Online (Sandbox Code Playgroud)

然而,我一直遇到:

IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException

我没有得到:文件在那里,与类位于同一目录中。它们甚至被编译成/out,我可以看到它就在那里。

我试过把它放进去,src/resources/applicationContext.xml但无济于事。

gly*_*ing 5

Spring 会在类路径的根目录中寻找它......

  • “与我的 Java 类相同的目录和包”不在类路径的根目录
  • src/resources 根本不在你的类路径上

如果你把它放进去,src/main/resources那么它将在你的类路径中(1)和(2)在你的类路径的根目录中。