栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java.io.FileNotFoundException: applicationContext.xml

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java.io.FileNotFoundException: applicationContext.xml

我的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");

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

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…-

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/917966.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号