1. 程式人生 > >【Error】:BeanCreationException: Error creating bean(Could not resolve resource location)

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

 遇到的一個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 配置內容如下:

 

解析當前 Url 需要在 classpath 後加個 “ * ” 即可,如下圖:

 

*注:前題還需要確保配置檔案路徑、大小寫、符號等,錯一個都不能找到系統配置檔案的。

 

當然上面個問題解決了,我還遇到其他的問題,一併記錄在這兒了,可以參考一下。

在專案的持久層,我使用的是 Mybatis 的 Mapper 將物件對映到資料庫,實現資料的持久方案,在執行的時候,出現 “org.apache.ibatis.binding.BindingException ”  的異常,關於這個問題的話,很簡單,到專案的 mapper 對映資料夾,可以看到為空,這是由於未載入對映檔案導致的,在專案的 pom.xml 配置檔案中加入如下圖:

 

為了方便各位的複製我將原始碼貼一下,在 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