項目框架springMVC + spring + mybatis,配置的事務是基於@transation註解的,最近發現有的模塊事務有作用,有的事務沒有生效,
spring 配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" default-lazy-init="true"> <description>Spring公共配置</description> <tx:annotation-driven proxy-target-class="true"/> <aop:aspectj-autoproxy proxy-target-class="true"/> <!-- 使用annotation 自動註冊bean, 並保證@Required、@Autowired的屬性被注入 --> <context:component-scan base-package="com.zeustel" use-default-filters="true"> <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> <!-- 讀取配置文件 ignore-unresolvable 爲true時可以配置多個context:property-placeholder加載多個properties配置文件--> <context:property-placeholder location="classpath*:/db.properties" ignore-unresolvable="false"/> <!-- c3p0數據源--> <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> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations"> <list> <value>classpath*:**/sql/*Mapping.xml</value> </list> </property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zeustel.dao.mapper" /> </bean> <!--spring聲明式事務管理--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 全局異常配置 --> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="java.lang.Exception">errors/500</prop> <prop key="java.lang.Throwable">errors/500</prop> </props> </property> <property name="statusCodes"> <props> <prop key="errors/500">500</prop> </props> </property> <!-- 設置日誌輸出級別,不定義則默認不輸出警告等錯誤日誌信息 --> <property name="warnLogCategory" value="WARN"></property> <!-- 默認錯誤頁面,當找不到上面mappings中指定的異常對應視圖時,使用本默認配置 --> <property name="defaultErrorView" value="errors/500"></property> <!-- 默認HTTP狀態碼 --> <property name="defaultStatusCode" value="500"></property> </bean> </beans>
嚐遍了網上所有方法,有的說spring-mvc.xml 和application-context..xml 掃描包原因, 有的說spring aop和事務先後順序問題,還有aop 使用cglib 的問題,還有數據庫表引擎的問題等等。。。。 還是一樣,突然想到了再 單元測試環境下試試 事務效果,
一開始單元測試類配置如下
把之前事務不生效的service 加上,一運行,居然可以啦!
還是很疑惑,爲什麼web容器下就不行呢, 繼續在網上找答案,也沒找到相關的,
忽然想到web容器和junit 環境配置的不通,就把web容器改加載的配置文件一個個 加上再運行, 後來發現了再加上
classpath*:spring-mvc.xml事務也可以生效, 排除這兩個文件的問題, 最後找到 shiro的配置文件 加上去
發現事務又不行了, 這時喜出望外,確定都是 shiro惹的禍! 網上一查, 原來是自定義的ShiroDbRealm 裏面注入的service 引起的
後來把 ShiroDbRealm裏面注入 service改爲注入dao,問題解決!