日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Spring3MVC+Mybatis3.1框架下的事务

發(fā)布時間:2025/4/5 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring3MVC+Mybatis3.1框架下的事务 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

最近做一個項目,項目的框架采用是Spring3MVC+MyBatis3.1。可是在開發(fā)過程中發(fā)現(xiàn)配置的事務不管用。

出現(xiàn)這個問題的現(xiàn)象是用Junit調試事務管用,而部署到Tomcat中就不管用了。先看看事務的配置:

<!--proxy-target-class="true"強制使用cglib代理 ? 如果為false則spring會自動選擇-->
<aop:aspectj-autoproxy proxy-target-class="true"/>

<!-- Transaction manager for a single JDBC DataSource -->
<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="save*" propagation="REQUIRED"/>
????</tx:attributes>
</tx:advice>


<aop:config>
<aop:pointcut expression="execution(public * com.luyou.platform.service.impl.*Impl.*(..))" id="pointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>


采用以下兩種方法調試:

一、Debug視圖

?1、Junit下的

發(fā)現(xiàn)配置事務的AOP已經包進來了。再看看Tomcat中運行的Debug截圖:

顯然AOP沒有被包進來。


二、Log4J的記錄:

Junit下的記錄:

Spring托管了事務。

Tomcat運行時的記錄:

Spring沒有托管事務。


從以上兩種方法的調試說明了,事務的配置是正確的,只是在部署到Tomcat中,沒有被托管。為什么會在Junit的時候就可以呢?得看看Junit的配置:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class})
@ContextConfiguration("classpath:applicationContext.xml")

這是Junit加載Spring容器的注解。ContextConfiguration會把applicationContext.xml的Bean都加載了,這就說明Tomcat在運行時沒有將applicationContext.xml的Bean加載進來。問了前輩,前輩的回話是這樣的:

切面配置在了root applicationContext的bean上了,而spring mvc會根據(jù)xxx-servelt.xml生成一個自己的applicationContext,他的父applicationContext為root applicatonContext,當mvc有自己的bean時便不再去向父context要bean,導致聲明事務無效。

看了前輩的這個郵件,我將applicationContext.xml中配置事務的AOP復制到XXX-servlet.xml中。再調試,Tomcat中運行項目事務被Spring托管了,也就是問題解決了!!

問題解決后查看了Spring3.1的Docs發(fā)現(xiàn)了以下的內容:

These inherited beans can be overridden in the servlet-specific scope, and you can define new scope-specific beans local to a given Servlet instance.
Context hierarchy in Spring Web MVC Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.

轉載于:https://my.oschina.net/xuqiang/blog/97633

總結

以上是生活随笔為你收集整理的Spring3MVC+Mybatis3.1框架下的事务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。