IDEA中Spring配置错误:class path resource [.xml] cannot be opened because it does not exist

IDEA中Spring配置错误:class path resource [.xml] cannot be opened because it does not exist
运行 Spring 项目时出现类似下面的错误信息

 class path resource [applicationContext.xml] cannot be opened because it does not exist

翻译:没有找到你的 .xml 配置文件

原因
在Test测试类中我使用的是:

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

获取配置文件。

ClassPathXmlApplicationContext()方法是在其所在的目录中寻找 .xml 配置文件。

注意: 这里指的是编译后的 .class 文件所在的目录,不是 .java 文件
解决
点击 File -> Project Structure,选择Modules -> Paths -> Output Paths
修改为 applicationContext.xml 文件所在目录。

项目中我是在使用sqlsessionfactory配置时,mapper指向xml文件目录时,报这个错误:
源码:

@Bean(name="rabbitProducerSqlSessionFactory")
	public SqlSessionFactory rabbitProducerSqlSessionFactory(DataSource rabbitProducerDataSource) {
		
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(rabbitProducerDataSource);
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		try {
			bean.setMapperLocations(resolver.getResources("classpath:mapping/*.xml"));
			SqlSessionFactory sqlSessionFactory = bean.getObject();
			sqlSessionFactory.getConfiguration().setCacheEnabled(Boolean.TRUE);
			return sqlSessionFactory;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

然后按照上面的操作后,问题解决;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值