【Error】:BeanCreationException: Error creating bean(Could not resolve resource location)

简介: 【Error】:BeanCreationException: Error creating bean(Could not resolve resource location)

遇到的一个javaweb的error:

1. [org.springframework.web.context.ContextLoader] - Context initialization failed
2. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'basedataController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'basedataService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ssm.dao.basedata.BasedataDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
3. Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'basedataDao' defined in file [E:\Java_Web_ProjectsXXX/BasedataDao.class]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-db.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.core.io.Resource[]' for property 'mapperLocations'; nested exception is java.lang.IllegalArgumentException: Could not resolve resource location pattern [classpath:com/mapper/*.xml]: class path resource [com/mapper/] cannot be resolved to URL because it does not exist

class path resource [sy/mapping/] cannot be resolved to URL because it does not exist :

找到相关代码所在配置文件:

解决方案一:将mapper.xml文件放到resources文件夹下

其实一直对classpath这个玩意儿一知半解,迷迷糊糊的。通过这次我对它认识更深了一步。

之前的印象:

在src目录下建个resources文件夹,丢配置文件.然后classpath:resources即可。

然后就是WEB-INF下的会自动被读取到。

现在:

看目录结构!source folder,和folder,还有package是有很大区别的!

按之前的理解,MybatisMapper下的mapper写法是:classpath:main/resources/MybatisMapper/*.xml

现在看来完全就是错误的,应该是classpath:MybatisMapper/*.xml

意思就是我们应该是按照源文件夹下的路径来写。(如果有错误或者不足欢迎指正,本人也是在学习)

(之后好好理解下源文件夹、文件夹、package的区别)

以及:

项目一定要按结构来划分,资源文件不要放在代码的package里面,那样是读不到的

(或者有方法能读到,但是肯定不如清晰的结构来得好)

其他:

classpath:/xxx 和 classpath:xxx是一样的

classpath:xxx 和 classpath*:xxx是不一样的,前者表示引入一个,后者表示引入多个。

解决方案二:修改配置文件,不移动mapper.xml文件:

Web.xml 配置内容如下:

713d4a324dc4429564c4e7d92063ee21_SouthEast.png

解析当前 Url 需要在 classpath 后加个 “ * ” 即可,如下图:

5c62f388f8260a825c01ba3874608b4b_SouthEast.png

*注:前题还需要确保配置文件路径、大小写、符号等,错一个都不能找到系统配置文件的。

当然上面个问题解决了,我还遇到其他的问题,一并记录在这儿了,可以参考一下。

在项目的持久层,我使用的是 Mybatis 的 Mapper 将对象映射到数据库,实现数据的持久方案,在运行的时候,出现 “org.apache.ibatis.binding.BindingException ”  的异常,关于这个问题的话,很简单,到项目的 mapper 映射文件夹,可以看到为空,这是由于未加载映射文件导致的,在项目的 pom.xml 配置文件中加入如下图:

cee0af1fcd388b0658015e8e4cb3fcc6_SouthEast.png

为了方便各位的复制我将源码贴一下,在 pom.xml 的末尾加入如下代码片段即可,具体内容如下:

1. <build>
2.  <resources>
3.    <resource>
4.      <directory>src/main/java</directory>
5.      <includes>
6.        <include>**/*.properties</include>
7.        <include>**/*.xml</include>
8.      </includes>
9.      <filtering>false</filtering>
10.     </resource>
11.     <resource>
12.       <directory>src/main/resources</directory>
13.       <includes>
14.         <include>**/*.properties</include>
15.         <include>**/*.xml</include>
16.       </includes>
17.       <filtering>false</filtering>
18.     </resource>
19.   </resources>
20. </build>

本文源自:

Maven项目启动失败:class path resource [config/spring/] cannot be resolved to URL because it does not exist_Hello_World_QWP的博客-CSDN博客

记一次mybatis的classpath踩坑记录_God_Ming的博客-CSDN博客_mybatis resource classpath


AIEarth是一个由众多领域内专家博主共同打造的学术平台,旨在建设一个拥抱智慧未来的学术殿堂!【平台地址:https://devpress.csdn.net/aiearth】 很高兴认识你!加入我们共同进步!

目录
相关文章
|
存储 缓存 文件存储
如何保证分布式文件系统的数据一致性
分布式文件系统需要向上层应用提供透明的客户端缓存,从而缓解网络延时现象,更好地支持客户端性能水平扩展,同时也降低对文件服务器的访问压力。当考虑客户端缓存的时候,由于在客户端上引入了多个本地数据副本(Replica),就相应地需要提供客户端对数据访问的全局数据一致性。
32697 79
如何保证分布式文件系统的数据一致性
|
前端开发 容器
HTML5+CSS3前端入门教程---从0开始通过一个商城实例手把手教你学习PC端和移动端页面开发第8章FlexBox布局(上)
HTML5+CSS3前端入门教程---从0开始通过一个商城实例手把手教你学习PC端和移动端页面开发第8章FlexBox布局
17748 20
|
设计模式 存储 监控
设计模式(C++版)
看懂UML类图和时序图30分钟学会UML类图设计原则单一职责原则定义:单一职责原则,所谓职责是指类变化的原因。如果一个类有多于一个的动机被改变,那么这个类就具有多于一个的职责。而单一职责原则就是指一个类或者模块应该有且只有一个改变的原因。bad case:IPhone类承担了协议管理(Dial、HangUp)、数据传送(Chat)。good case:里式替换原则定义:里氏代换原则(Liskov 
36680 19
设计模式(C++版)
|
存储 编译器 C语言
抽丝剥茧C语言(初阶 下)(下)
抽丝剥茧C语言(初阶 下)
|
机器学习/深度学习 人工智能 自然语言处理
带你简单了解Chatgpt背后的秘密:大语言模型所需要条件(数据算法算力)以及其当前阶段的缺点局限性
带你简单了解Chatgpt背后的秘密:大语言模型所需要条件(数据算法算力)以及其当前阶段的缺点局限性
24757 14
|
机器学习/深度学习 弹性计算 监控
重生之---我测阿里云U1实例(通用算力型)
阿里云产品全线降价的一力作,2023年4月阿里云推出新款通用算力型ECS云服务器Universal实例,该款服务器的真实表现如何?让我先测为敬!
36660 15
重生之---我测阿里云U1实例(通用算力型)
|
SQL 存储 弹性计算
Redis性能高30%,阿里云倚天ECS性能摸底和迁移实践
Redis在倚天ECS环境下与同规格的基于 x86 的 ECS 实例相比,Redis 部署在基于 Yitian 710 的 ECS 上可获得高达 30% 的吞吐量优势。成本方面基于倚天710的G8y实例售价比G7实例低23%,总性价比提高50%;按照相同算法,相对G8a,性价比为1.4倍左右。
|
存储 算法 Java
【分布式技术专题】「分布式技术架构」手把手教你如何开发一个属于自己的限流器RateLimiter功能服务
随着互联网的快速发展,越来越多的应用程序需要处理大量的请求。如果没有限制,这些请求可能会导致应用程序崩溃或变得不可用。因此,限流器是一种非常重要的技术,可以帮助应用程序控制请求的数量和速率,以保持稳定和可靠的运行。
29837 52

热门文章

最新文章

下一篇
开通oss服务