遇到的一个javaweb的error:

[org.springframework.web.context.ContextLoader] - Context initialization failed
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=)}
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 配置内容如下:

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

 

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

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

 

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

 

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

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

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

 

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

<build>
	<resources>
		<resource>
			<directory>src/main/java</directory>
			<includes>
				<include>**/*.properties</include>
				<include>**/*.xml</include>
			</includes>
			<filtering>false</filtering>
		</resource>
		<resource>
			<directory>src/main/resources</directory>
			<includes>
				<include>**/*.properties</include>
				<include>**/*.xml</include>
			</includes>
			<filtering>false</filtering>
		</resource>
	</resources>
</build>

 

 

本文源自:

https://blog.csdn.net/Hello_World_QWP/article/details/79398462

https://blog.csdn.net/jinzhencs/article/details/50595476

 

 

10-07 20:48