【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】 很高兴认识你!加入我们共同进步!

目录
相关文章
|
4月前
|
XML Android开发 数据格式
Error obtaining Ul hierarchy Reason_ Error while obtaining Ul hierarchy XML file
Error obtaining Ul hierarchy Reason_ Error while obtaining Ul hierarchy XML file
32 0
|
4月前
|
XML Android开发 数据格式
Error obtaining Ul hierarchy Reason: Error while obtaining Ul hierarchy XML file
Error obtaining Ul hierarchy Reason: Error while obtaining Ul hierarchy XML file
24 1
|
10月前
|
程序员
解决报错Exception encountered during context initialization
程序员不是在去生产bug的路上,那就是在去解决bug的路上。
98 1
|
7月前
|
Java Spring
BeanCreationException: Error creating bean with name ‘configurationPropertiesBeans‘ defined in class
BeanCreationException: Error creating bean with name ‘configurationPropertiesBeans‘ defined in class
106 0
|
11月前
|
关系型数据库 MySQL C++
Error:error C2601: ‘b‘ : local function definitions are illegal error C2063: ‘b‘ : not a function
Error:error C2601: ‘b‘ : local function definitions are illegal error C2063: ‘b‘ : not a function
126 0
|
Nacos
Nacos报错:Error creating bean with name ‘authFilterRegistration‘ defined in class path resource
Nacos报错:Error creating bean with name ‘authFilterRegistration‘ defined in class path resource
366 0
|
Java 数据库连接 网络安全
Error creating bean with name ‘attrAttrgroupRelationController‘
Error creating bean with name ‘attrAttrgroupRelationController‘
Error creating bean with name ‘attrAttrgroupRelationController‘
|
Java API Spring
A component required a bean named xxx that could not be found
A component required a bean named xxx that could not be found
A component required a bean named xxx that could not be found
How to resolve Unable to load groups error message
When you open Fiori launchpad, you can only see empty screen with the following error message:
How to resolve Unable to load groups error message
why my cloudDatabaseconfig bean Initialization failed
why my cloudDatabaseconfig bean Initialization failed
96 0
why my cloudDatabaseconfig bean Initialization failed