ssm中mapper注入失敗的傳奇經歷
最近因為要測試一個功能,需要用最短的時間來啟動服務,開啟測試程式,但平常所用的框架中已經集成了各種三方的東西,想著那就再重新搭建一個最簡單的ssm框架吧。
搭建可參考:簡單ssm最新搭建
搭建過程並不麻煩,整合springmvc測試成功,介面正常呼叫,最後整合mybatis後,在service中注入呼叫時出現了問題,啟動服務時報錯如下:
No qualifying bean of type 'com.test.mapper.TpmTestLogMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
一、bug解決
首先先說一下我的錯誤原因:web.xml中沒有配置監聽器listener,也就是下邊這些程式碼:
(其實配置了,但是不知道為什麼最後發現是註釋掉的,真的 頭大!!!)
<!-- Spring監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
(文末附有完整的web.xml配置,直接到文末)
那麼為什麼沒有配置監聽,就導致mybatis注入失敗了呢,這就不得不提到監聽配置其中的一個作用了,監聽載入容器配置檔案,也就是Spring的配置檔案applicationContext.xml。
載入流程: tomcat服務啟動時,會首先載入web.xml,然後監聽器會預設去載入Spring的相關配置檔案,從而建立spring容器中的各個bean元件。
預設載入的配置檔案路徑是:WEB-INF/applicationContext.xml,當然很多時候我們為了專案結構的清晰,會將此配置檔案放在resource下,這時也需要在web.xml中額外配置
<!--spring的其他配置檔案(包括mybatis配置檔案) --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:spring-applicationContext.xml </param-value> </context-param>
錯誤原因總結:啟動檔案web.xml中沒有配置監聽器,導致我的Spring相關配置檔案沒有載入,所以其中引入的spring-mybatis配置檔案更不會載入,bean沒有建立成功,最終導致了在service中呼叫時注入失敗。
二、排查思路
排查過程中的思路:
-
spring-mybatis整合配置檔案路徑是否正確,能否被掃描到
-
mapper.java和mapper.xml之間有沒有聯絡起來,資料庫配置是否正確,mapper介面是否可以掃描到(檢視配置檔案)
- 有沒有掃描到mapper.xml檔案(檢視編譯檔案,及對應路徑是否正確)
1、檢查spring-mybatis配置檔案,在web.xml中是否可以掃描到:在serviceImpl中手動獲取,看能否獲取到
@Override
public void ceshi(TpmTestLog tpmTestLog) {
//手動獲取建立
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring-mybatis.xml");
TpmTestLogMapper bean = ac.getBean(TpmTestLogMapper.class);
bean.insert(tpmTestLog);
System.out.println(bean.toString());
}
測試是否可以成功獲取到,可以,則表示mybatis路徑正確,可以在web.xml中被正常掃描。
2、檢查mybatis配置檔案是否正確:主要是下方標紅的兩項配置會造成此錯誤
<!-- 讀取配置檔案資訊 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:jdbc.properties"/> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 資料庫基本配置 --> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 初始化連線數量 --> <property name="initialSize" value="${jdbc.initialSize}"/> <!-- 最大併發連線數量 --> <property name="maxActive" value="${jdbc.maxActive}"/> <!-- 最小空閒連線數 --> <property name="minIdle" value="${jdbc.minIdle}"/> <!-- 配置獲取連線等待超時的時間 --> <property name="maxWait" value="${jdbc.maxWait}" /> <!-- 超過時間限制是否回收 --> <property name="removeAbandoned" value="${jdbc.removeAbandoned}" /> <!-- 超過時間限制多長 --> <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}" /> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" /> <!-- 配置一個連線在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" /> <!-- 用來檢測連線是否有效的sql,要求是一個查詢語句--> <property name="validationQuery" value="${jdbc.validationQuery}" /> <!-- 申請連線的時候檢測 --> <property name="testWhileIdle" value="${jdbc.testWhileIdle}" /> <!-- 申請連線時執行validationQuery檢測連線是否有效,配置為true會降低效能 --> <property name="testOnBorrow" value="${jdbc.testOnBorrow}" /> <!-- 歸還連線時執行validationQuery檢測連線是否有效,配置為true會降低效能 --> <property name="testOnReturn" value="${jdbc.testOnReturn}" /> <property name="logAbandoned" value="true" /> <!-- 配置監控統計攔截的filters,wall用於防止sql注入,stat用於統計分析 --> <property name="filters" value="stat" /> </bean> <!-- MyBatis SqlSessionFactoryBean 配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 自動掃描Mapping.xml檔案 --> <property name="mapperLocations" value="classpath:com/test/mapper/xml/*.xml"/> <!-- 配置MyBaties全域性配置檔案:mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- 掃描model包 xml中parameterType就可以使用類名,不用全路徑 --> <property name="typeAliasesPackage" value="com.test.model"/> </bean> <!-- 載入 mapper.xml對應的介面 配置檔案 --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 給出需要掃描mapper介面包 --> <property name="basePackage" value="com.test.mapper"/> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
3、mapper.xml檔案是否可以被正確掃描到,編譯後的檔案中是否存在以及路徑是否正確
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!--spring的其他配置檔案(包括mybatis配置檔案) --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:spring-applicationContext.xml </param-value> </context-param> <!-- 編碼過濾器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring監聽器 監聽載入相關配置檔案--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--防止Spring記憶體溢位監聽器--> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!--Spring MVC servlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> <welcome-file>/index.html</welcome-file> <welcome-file>/reg.html</welcome-file> </welcome-file-list> </web-app>
&n