通過逆向工程建立mapper和bean,做單元測驗MyBatis是成功的

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'firstCRUD': Unsatisfied dependency expressed through field 'crud'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.springMVC.CRUD.service.CRUD' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
目錄結構

設定MVC路徑

service層

applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.springMVC.CRUD">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- 引入組態檔來配置資料庫-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- spring用來控制業務邏輯等等 資料源 事務控制-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="https://bbs.csdn.net/topics/${jdbc.driverClass}"/>
<property name="jdbcUrl" value="https://bbs.csdn.net/topics/${jdbc.jdbcUrl}"/>
<property name="user" value="https://bbs.csdn.net/topics/${jdbc.user}"/>
<property name="password" value="https://bbs.csdn.net/topics/${jdbc.password}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 使用的資料源-->
<property name="dataSource" ref="dataSource"/>
<!-- 指定全域組態檔的位置-->
<property name="configLocation" value="https://bbs.csdn.net/topics/classpath:MyBatisConfig.xml"/>
<!-- 指定mapper檔案的位置,這里是classpath下的mapper檔案夾里所有的xml檔案-->
<property name="mapperLocations" value="https://bbs.csdn.net/topics/classpath:com/springMVC/CRUD/dao/mapper/*.xml"/>
</bean>
<!-- 掃描MyBatis介面的實作,加入到IOC容器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定mapper介面的包名-->
<property name="basePackage" value="https://bbs.csdn.net/topics/com.springMVC.CRUD.dao"/>
</bean>
<!-- 配置一個可以執行批量操作的bean->
<bean id="sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
<constructor-arg name="executorType" value="https://bbs.csdn.net/topics/BATCH"/>
</bean>
<!-- 事務管理器-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 控制資料庫的事務,引進property屬性里面-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 開啟基于注解的事務:推薦比較重要的使用配置的方式.而不是注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<aop:config>
<!-- 切入點運算式-->
<aop:pointcut id="txPoint"
expression="execution(* com.springMVC.CRUD.service..*(..))"/>
<aop:advisor advice-ref="transactionManager2" pointcut-ref="txPoint"/>
</aop:config>
<!-- 配置事務增強,如何注入-->
<tx:advice id="transactionManager2">
<tx:attributes>
<!-- 所有方法都是事務方法-->
<tx:method name="*"/>
<!-- 以get開始的所有方法,認為只是查詢,設定為只讀,做出優化-->
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>
dispatcherServlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<context:component-scan base-package="com.springMVC.CRUD.controller" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!--配置視圖決議器:如何把handler方法回傳值決議為實際的物理視圖-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置物理路徑"prefix:前綴 suffix:后綴 中間是你方法的回傳值-->
<property name="prefix" value="https://bbs.csdn.net/WEB-INF/jspLocale/views/"/>
<property name="suffix" value="https://bbs.csdn.net/topics/.jsp"/>
</bean>
<!-- 將springMVC不能處理的資源交給Tomcat,靜態資源-->
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
</beans>
uj5u.com熱心網友回復:
CRUD都沒作為bean被創建出來,你檢查兩個mapper到底有沒有注入。你這里scanner configurer都沒指定session工廠,你配置xml檔案都在這里,spring怎么知道你的mapper介面接誰...
改成這樣試試:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定mapper檔案的位置,這里是classpath下的mapper檔案夾里所有的xml檔案-->
<property name="mapperLocations" value="https://bbs.csdn.net/topics/classpath:com/springMVC/CRUD/dao/mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定mapper介面的包名-->
<property name="basePackage" value="https://bbs.csdn.net/topics/com.springMVC.CRUD.dao"/>
<property name="sqlSessionFactoryBeanName" value="https://bbs.csdn.net/topics/sqlSessionFactory"/>
</bean>
還有,一般service都是介面注入,用介面去接上溯到介面的實作類。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/146534.html
標籤:Web 開發
上一篇:Maven Springboot
下一篇:java如何高性能網路爬蟲
