我的applicationContext.xml放在src/main/resource目录下,加载ApplicationContext时用了FileSystemXmlApplicationContext

ApplicationContext ac = FileSystemXmlApplicationContext("applicationContext.xml");

完整报错信息

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [项目根目录\applicationContext.xml]; 
nested exception is java.io.FileNotFoundException: applicationContext.xml

原因其实很简单,applicationContext.xml不在根目录下。但是我明明记得以前这么写是不会报错的,为什么这回就找不到applicationContext.xml了呢,翻了之前写的代码,发现之前写的是

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

ClassPathXmlApplicationContextFileSystemXmlApplicationContext还是有区别的
ClassPathXmlApplicationContext默认从类路径(可以粗暴地理解为src/main/resource)中加载ApplicationContextFileSystemXmlApplicationContext默认从文件系统中加载,默认为当前工作目录的相对路径,像我这种情况,下面几种写法等价:

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ApplicationContext ac = new FileSystemXmlApplicationContext("src/main/resource/applicationContext.xml");
ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");

确切地说,这个问题和maven有关
ClassPathXmlApplicationContext从class path中加载ApplicationContext,maven默认将资源放在src/main/resources目录下,编译后.class文件和资源文件会放到target/classes目录下,打包后将放到maven默认的WEB-INF/classes目录下,所以这里classpath是WEB-INF/classes
对于ClassPathXmlApplicationContext,classpath:前缀可省略

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

若想从绝对路径中加载,需要加上file:前缀

ApplicationContext ac = new ClassPathXmlApplicationContext("file:D:/applicationContext.xml");

FileSystemXmlApplicationContext从文件系统中加载ApplicationContext,默认相对于当前的工作路径

ApplicationContext ac = new FileSystemXmlApplicationContext("src/main/resource/applicationContext.xml");

若要表示绝对路径,需要加上file:前缀,若路径以盘符开头,则不需要file:前缀也表示绝对路径

ApplicationContext ac = new FileSystemXmlApplicationContext("D:/applicationContext.xml");

也可以加上classpath:前缀,从class path中加载ApplicationContext

ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");

在写测试类的时候,如果用到@ContextConfiguration,也会出现类似的报错

java.lang.IllegalStateException: Failed to load ApplicationContext

同样地,applicationContext.xml放在src/main/resource目录下,报错代码

@ContextConfiguration(locations = “applicationContext.xml”)

而以下几种写法不报错

@ContextConfiguration(locations =/applicationContext.xml”)
@ContextConfiguration(locations = “classpath:applicationContext.xml”)
@ContextConfiguration(locations = “classpath:/applicationContext.xml”)

参考
[1] https://blog.csdn.net/Vermont_/article/details/109118810
[2] https://maven.apache.org/pom.html#Resources
[3] https://docs.spring.io/spring-framework/docs/3.0.x/spring-framework-reference/html/resources.html
[4] https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/support/ClassPathXmlApplicationContext.html
[5] https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/support/FileSystemXmlApplicationContext.html
[6] https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/util/TestContextResourceUtils.html#convertToClasspathResourcePaths-java.lang.Class-boolean-java.lang.String…-

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐