【发布时间】:2022-01-24 03:52:22
【问题描述】:
我对 Java 比较陌生,正在尝试学习 Spring 框架。我有以下代码
public class EmployeeTest {
public static void main(String ...args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("my-config.xml");
Employee employee = (Employee) ctx.getBean("emp");
System.out.println(employee.getId());
System.out.println(employee.getName());
}
}
my-config.xml 位于EmployeeTest 类本身所在的位置。但是,当我运行此代码时,出现以下异常:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [my-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [my-config.xml] cannot be opened because it does not exist
按照一些文章的建议,我尝试将 my-config.xml 放在 src/main/java 下,但还是没有成功
我正在使用 IntelliJ Idea 作为编辑器。 非常感谢任何提示
【问题讨论】:
-
如果你试图通过类路径访问配置文件,它需要在
src/main/resources