java.io.FileNotFoundException: class path resource [XXXXX.xml] cannot be opened

java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exit.

在用maven管理的spring项目中做单元测试时候,加载不了spring的配置文件。出现该问题的原因是,用maven创建的项目,必须把spring的配置文件beans.xml或者applicationContext.xml放到系统根目录下,做单元测试时候用

ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
		Girl girl=(Girl)applicationContext.getBean("girl");

才能正确加载spring的配置文件,才能正确获取bean。不知道这个定律在其他电脑的环境下是否适合,撸主在两台计算机上都是通过这种方式配好的。

maven项目的系统根目录默认是src/main/java和src/main/resources,而不是src,所以beans.xml文件必须放到src/main/java和src/main/resources下面的文件夹或者包中,否则就会报上面的错误:java.io.FileNotFoundException:
class path resource [beans.xml] cannot be opened because it does not exit.

还有一点,要注意的是,如果beans.xml不是直接放到了src/main/java和src/main/resources路径下,在读取beans文件时候,还要加上路径,如beans.xml放在了src/main/java/conf路径下,那么加载时候要这样写:

ApplicationContext applicationContext=new ClassPathXmlApplicationContext("conf/beans.xml");

否则就会报错:java.io.FileNotFoundException:
class path resource [beans.xml] cannot be opened because it does not exit.

而下面这种写法,是把beans.xml直接放到了src/main/java和src/main/resources路径下

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

java.io.FileNotFoundException: class path resource [XXXXX.xml] cannot be opened

时间: 2024-10-24 07:03:36

java.io.FileNotFoundException: class path resource [XXXXX.xml] cannot be opened的相关文章

parsing XML document from class path resource [applicationtext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationtext.xml] cannot be opened because it does not e

控制台异常: parsing XML document from class path resource [applicationtext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationtext.xml] cannot be opened because it does not exist 报错原因是 xml名字可能写错了 原文地址:https://www.cnbl

解决 java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist

前几天好好运行的web项目突然跑不起来了 报这个错误 2019-09-16 19:18:29 [ RMI TCP Connection(3)-127.0.0.1:357 ] - [ ERROR ] Context initialization failed org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource

Caused by: java.io.FileNotFoundException: class path resource [mappers/*.xml] cannot be opened because it does not exist

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'methodValidationPostProcessor' defined in class path resource [org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]: Unsa

nested exception is java.io.FileNotFoundException: class path resource [spring-mybatis.xml] cannot be opened

idea+maven+springmvc项目,做单元测试报错,找不到配置文件. @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring-mybatis.xml"}) public class TestMybatis { private static Logger logger = LoggerFactory.getLogger(TestMybatis.cla

Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be ope

1.错误描述 java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99) at org.springframework.test.context.DefaultTestContext.

cxf(3.1.1) 异常Caused by: java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf-extension-soap.xml]

Caused by: java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf-extension-soap.xml] cannot be opened because it does not exist用的最新版本,这个版本中 cxf-extension-soap.xml 不需要了直接在配置文件中删除即可 现在只需要引两个文件 <import resource="classpath:META-INF/cxf/

解决CXF的java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf-extension-soap.xml] cannot be opened because it does not exist

以下是错误信息 九月 25, 2017 8:22:04 下午 org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh 信息: Refreshing Root WebApplicationContext: startup date [Mon Sep 25 20:22:04 CST 2017]; root of context hierarchy 九月 25, 2017 8:22:04 下午 or

java.io.FileNotFoundException: class path resource ..cannot be opened because it does not exist

java.io.FileNotFoundException: class path resource ..cannot be opened because it does not exist 异常如下: java.io.FileNotFoundException: class path resource [com/jxust/zjh/model/Account.hbm.xml] cannot be opened because it does not exist 在spring中我这样配置<pr

nested exception is java.io.FileNotFoundException: class path resource [spring/spring-datasource-mog

spring单元测试时发现的问题: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring/spring-datasource-mogon.xml]; nested exception is java.io.FileNotFoundException: class path resource [