class path resource [config/mybatis/mapper3/] cannot be resolved to URL because it does not exist

SpringBoot项目启动失败: java.io.FileNotFoundException: class path resource [config/mybatis/mapper3/] cannot be resolved to URL because it does not exist

之前配置的多个数据源都没有问题,如上图,3是新加的,还没有文件

这个是无法解析当前的URL,做出如下修改,classpath 后多加一个“ * ”,修复了这个错误。

修改后:

问题到了这一步,你难得不好奇为啥一模一样的配置,有*跟没*有啥区别,为啥会有这种错误?

又到了翻源码的环节。

如果配置的是

classpath:config/mybatis/mapper3/*.xml

java.io.FileNotFoundException:class path resource [config/mybatis/mapper3/] cannot be resolved to URL because it does not exist

但如果配置的是classpath*:config/mybatis/mapper3/*.xml , 这个方法就不报错了么,我们来一探究竟

直接跳过了for循环,关键在下面这个方法,CLASSPATH_ALL_URL_PREFIX = CLASSPATH_ALL_URL_PREFIX

加*的走到了这个方法

// all class path resources with the given name
 return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));

返回的Resource[] rootDirResources   = new Resource[0] ;

不加*的走了下面这个

 // a single resource with the given name 
 return new Resource[] {getResourceLoader().getResource(locationPattern)};

classpath:config/mybatis/mapper3/

new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());

返回的 Resource[] rootDirResources 有一条数据 [class path resource [config/mybatis/mapper3/]]


版权声明:本文为qq_39308071原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。