问题:Spring 项目中 AppConfig.xml 出现 FileNotFoundException

问题描述 投票:0回答:1

项目图片

我正在开发一个名为 MavenfirstProject 的基于 Maven 的 Spring 项目。 文件夹结构如下:

src
├── main
│   ├── java
│   │   └── in.sp.main.App.java
│   └── resources
│       └── AppConfig.xml
└── test

问题:尝试使用 ClassPathXmlApplicationContext 加载 AppConfig.xml 文件时,我不断收到 FileNotFoundException。

以下是相关错误:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [in/sp/resources/AppConfig.xml]

...

Caused by: java.io.FileNotFoundException: class path resource [in/sp/resources/AppConfig.xml] cannot be opened because it does not exist

我尝试过的:

文件位置:文件放在src/main/resources下,应该包含在类路径中。 加载配置文件:我使用以下代码加载文件:

 String file = "in/sp/resources/AppConfig.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(file);

清理和重建项目:我在 IntelliJ 中清理并重建了项目,以确保文件正确复制到目标/类。 观察到的行为:即使执行了这些步骤,异常仍然存在,并且 Spring 仍然尝试在 in/sp/resources 目录下而不是在根类路径中查找该文件。

java spring maven
1个回答
0
投票

修改为这个

 String file = "classpath:AppConfig.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(file);
© www.soinside.com 2019 - 2024. All rights reserved.