spring mvc中AOP事物的配置,AOP事物不生效解決辦法

spring mvc中很多時候都會配置掃描其,如

<context:component-scan base-package="XX.XX.*" />

掃描spring mvc的註解,此時如果在其他spring配置文件中,配置AOP的事物,如本人使用mybatis,在spring-mybatis中有如下配置,配置事物處理,處理事物

<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
    </bean>
	<tx:advice id="userTxAdvice" transaction-manager="transactionManager">  
	  <tx:attributes>  
	      <tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>  
	      <tx:method name="add*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />  
	      <tx:method name="insert*" propagation="REQUIRED" isolation="SERIALIZABLE" read-only="false" rollback-for="java.lang.Exception" />  
	      <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />  
	      <tx:method name="modify*" propagation="REQUIRES_NEW" read-only="false" rollback-for="java.lang.Exception" />  
	      <tx:method name="find*" propagation="SUPPORTS"/>  
	      <tx:method name="get*" propagation="SUPPORTS" isolation="SERIALIZABLE"/>  
	      <tx:method name="select*" propagation="REQUIRED" isolation="SERIALIZABLE"/>  
	  </tx:attributes>  
	</tx:advice>  
	<aop:config>
	    <aop:pointcut id="pc" expression="execution( * *.service..*.*(..))" /> <!-- 把事務控制在Service層 -->  
	    <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />  
	</aop:config>  
此時發現service 層的事物並沒有生效,

度娘後結合項目分析,得出結論,若不正確歡迎指正,由於初始化時先加載了spring-mvc.xml配置文件此時由於配置的掃描器包含了service層,此時並沒有加入AOP,所有AOP不能對事物進行管理,在詞加載spring-mybatis配置文件,此時雖然有aop但是,因爲容器的原因,將會導致,使用的service並不是後面加載的service,所以雖然配置了事物管理卻不生效

解決辦法:

添加列外,感覺有點像spring mvc 的interceptor添加例外,

通過exclude-filter標籤添加例外如:

 <context:component-scan  base-package="pack.*" > 
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
      </context:component-scan> 

對其進行列外處理,解決該問題。


網頁都說使用此方式,將會導致try..catch的事物處理不生效,本人沒有驗證,

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章