springmvc mybatis 註解的方式 事務不起作用解決方案
阿新 • • 發佈:2019-02-03
1、使用springmvc導致事務不起作用時,首先檢視是否元件重複掃描問題導致
由於採用的是SpringMVC、 MyBatis,故統一採用了標註來宣告Service、Controller。伺服器啟動時的載入配置檔案的順序為web.xml---root-context.xml(Spring的配置檔案)---servlet-context.xml(SpringMVC的配置檔案),由於root-context.xml配置檔案中Controller會先進行掃描裝配,但是此時service還沒有進行事務增強處理,得到的將是原樣的Service(沒有經過事務加強處理,故而沒有事務處理能力),所以我們必須在root-context.xml中不掃描controller,同樣,在springmvc的配置檔案中不掃描service
application.xml
spring-mvc.xml<context:property-placeholder location="classpath:jdbc.properties"/> <context:component-scan base-package="cn.edu.nuc"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 資料來源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!--mybatis的sqlsessionFactory配置--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="mapperLocations" value="classpath:cn/edu/nuc/mybatis/mapper/*.xml" /> </bean> <!-- 事務 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/> <tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/> <tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="transactionPointCut" expression="execution(* cn.edu.nuc.service..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointCut" /> </aop:config>
2、注意:檢視mysql資料庫引擎是否支援事務<!-- 啟動註解驅動的Spring MVC功能,註冊請求url和註解POJO類方法的對映--> <mvc:annotation-driven /> <!-- 對模型檢視名稱的解析,在請求時模型檢視名稱新增前後綴 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 啟動包掃描功能,以便註冊帶有@Controller、@Service、@repository、@Component等註解的類成為spring的bean --> <context:component-scan base-package="cn.edu.nuc"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan>
3、檢視依賴注入:
1)spring預設使用jdk的動態代理新增事務/依賴注入,所以在往pring ioc容器中註冊bean時,需要再實現類上加@Service、@Repository等。
2)BaseDaoImpl、BaseDao 上不用使用註解。
3)在依賴注入的時候,要使用介面的方式,eg:
@Autowired
public DemoService demoServiceImpl;