我尝试在 Spring 应用程序中创建一个 bean,但在将 XML 配置文件加载到 ClassPathXmlApplicationContext 时遇到 IOException。
测试类代码:
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/satwik/spring/config.xml");
Employee employee = (Employee) context.getBean("employee");
System.out.println(employee);
}
}
无法确定为什么我收到此异常,config.xml 文件显然位于类路径(com/satwik/spring)内,并且我在创建 ClassPathXmlApplicationContext 时提供了相同的文件,如上面的代码所示。
项目类路径:
但是我遇到了以下 IOException:
at com.satwik.spring.Test.main(Test.java:11)
Caused by: java.io.FileNotFoundException: class path resource [com/satwik/spring/config.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
... 13 more
... 13 more
我也尝试只保留 config.xml 而不是完整的类路径,但得到了相同的 IOException。
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
ClassPathXmlApplicationContext
默认情况下会在以下文件夹中查找资源:src/main/resources/
。
我认为这个已经回答的问题会有帮助Spring无法找到bean xml配置文件,当它确实存在时