Spring Boot资源获取失败:class path resource cannot be opened because it does not exist
一、问题背景
在配置SpringBoot多数据源时,报mapper.xml文件找不到,但是看了编译后的目录下确实有文件。
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'class path resource [mapper/db01/*Mapper.xml]'; nested exception is java.io.FileNotFoundException: class path resource [mapper/db01/*Mapper.xml] cannot be opened because it does not existat org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622)... 42 common frames omitted
Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: 'class path resource [mapper/db01/*Mapper.xml]'; nested exception is java.io.FileNotFoundException: class path resource [mapper/db01/*Mapper.xml] cannot be opened because it does not existat org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:595)at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:475)at org.mybatis.spring.SqlSessionFactoryBean.getObject(SqlSessionFactoryBean.java:615)
二、解决方法
百度看了很多帖子,依然没有解决,最后仔细看获取xml的方法时发现用错了
public SqlSessionFactory sqlSessionFactory(@Qualifier("db02DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource);sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResource("classpath:/mapper/db02/*Mapper.xml"));return sqlSessionFactoryBean.getObject();}
getResource方法中少了一个s,正确的方法是getResources
- getResource:获取文件系统文件、从类路径下获取指定的文件
- getResources:获取所有类路径下的指定文件、使用通配符获取满足某种格式的文件
public SqlSessionFactory sqlSessionFactory(@Qualifier("db02DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource);sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/db02/*Mapper.xml"));return sqlSessionFactoryBean.getObject();}
修改后重新启动项目,正常运行,无报错。
Spring Boot资源获取失败:class path resource cannot be opened because it does not exist相关推荐
- 点击项目任意页面都是404,且报错class path resource [] cannot be opened because it does not exist等
问题:点击项目任意页面都是404,且报错class path resource [] cannot be opened because it does not exist等 产生:从svn拉取项目运行 ...
- Spring boot Freemarker 获取ContextPath的方法
Spring boot Freemarker 获取ContextPath的两种方法: 1.自定义viewResolver,Spring boot中有一个viewResolver,这个和配置文件中的师徒 ...
- Idea创建Spring Boot项目超时失败的解决方案及Maven改SpringBoot
前言 说起IntelliJ IDEA 可谓是作为一名开发者必须知道的神器了,便捷高效,但不知道是升级了新的Idea软件,还是网络不通畅缘故,笔者这两天老遇到一个创建超时的问题. 用 IDEA 创建 S ...
- springboot报错:class path resource cannot be opened because it does not exits
问题描述:springboot报错:在使用@PropertySource注解读取properties文件时,报错:class path resource cannot be opened becaus ...
- 【web安全】Spring boot heapdump获取敏感信息
一.概述 微服务是目前较为热门的技术,Spring boot 是 Spring 的一套快速配置脚手架,可以基于Spring boot 快速开发单个微服务,微服务的特点决定了功能模块分布式部署,在不同的 ...
- Spring Boot : 资源加载器
1.美图 2.概述 前言参考: 源码:Spring boot 主程序的功能(启动流程) ResourceLoader接口,在 Spring 中用于加载资源,通过它可以获取一个Resouce 对象.使用 ...
- 解决spring的xml文件cannot be open ( class path resource cannot be opened)
xml文件一直显示cannot be open 报错语句有一条: class path resource [bean2.xml] cannot be opened because it does no ...
- 记一次spring boot项目启动失败
最近在macos下启动spring boot 项目的时候启动失败,报错信息如下: org.postgresql.util.PSQLException: 尝试连线已失败.at org.postgresq ...
- Spring Boot当中获取request的三种方式
本篇博客主要记录request相关知识,也是开发当中经常遇到的,感兴趣的跟小编一起学习吧! 目录 一.请求过程 二.获取request的三种方式 2.1.可以封装为静态方法 2.2.controlle ...
- android 手机资源获取失败,安卓手机刷机原理方法介绍刷机失败解决方案
5风险 说到风险但凡刷机都带有一定的风险关键看你能不能把风险最小化甚至去掉了.首先肯定的是正常的刷机操作是不会损坏手机硬件的.其次刷机可以解决手机有些莫名其妙的毛病.升级手机硬件驱动就象升级电脑的BI ...
最新文章
- 2021年大数据Spark(二十五):SparkSQL的RDD、DF、DS相关操作
- Vs.net2008 下 Ajaxpro 使用
- 我所认识的SAP系统
- 一步搞清楚多态与类初始化的底层原理
- vue 数组数据改变 视图不更新解决方案
- iOS 之 UITextField
- python全景图像拼接_Python+OpenCV实现图像的全景拼接
- C语身教程第十章:文件(8)
- HTML5+CSS3设计界面
- SpingMVC 注解@RequestMapping、@SuppressWarnings、@Scheduled 定时器
- 基于canoe的bootload刷写程序
- hutool实战(带你掌握里面的各种工具)目录
- 电感值、电容值的理解(上)
- 备忘录模式(设计模式_20)
- 基于rancheros离线安装Habor以及Rancher
- HTML页面悬浮球,html滑动仿悬浮球菜单
- 打电话流程(打电话时,从拨出电话到接听电话的几秒经历了什么)
- 可在线听歌却不能下载,破解后可下载有限制的音乐作品
- 截图软件shutter
- 同学用javascript写好了论坛自动灌水机。
热门文章
- 最后剩下的,只有随遇而安,偶尔我会想起你:伤感日志
- Ubuntu20与win10共享文件夹
- 移动硬盘linux读取失败,无法读取移动硬盘|无法识别的6种修复方法插图
- 【历史上的今天】12 月 12 日:英特尔创始人出生;PSP 发布;双十二购物狂欢节
- Wait for me和waitting for me有什么区别?
- mysql 引擎 切换_Mysql表引擎的切换
- 友盟集成微信授权登录,切换微信账号,登录无法切换微信信息问题
- 定义一个接口ITest,接口中有3个抽象方法如下。
- 概率熵物质混乱能量耗散信息熵
- python编程游戏-9个Python编程小游戏,有趣又好玩,简直太棒了