【重要】spring+springMVC,JdbcTemplate聲明式事務失效,原因以及解決辦法

【重要】spring+springMVC,JdbcTemplate聲明式事務失效,原因以及解決辦法

 

我的分析:由於配置了多份xml配置文件,導致了會生成父子容器,applicationContext.xml生成父容器,servlet-context.xml生成子容器。

 

@Service實例 本應由父容器掃描,結果由子容器生成。解決辦法參照第三點,指定哪些不掃描。

 

--------------------------------以下是轉載內容------------------------------

一.聲明式事務配置:

 

        <beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

                  <propertyname="dataSource" ref="dataSource" />

        </bean>

   <tx:adviceid="txAdvice"transaction-manager="transactionManager">

           <tx:attributes>

                       <tx:method name="add*"propagation="REQUIRED"read-only="false"/>

                       <tx:method name="del*"propagation="REQUIRED"read-only="false"/>

                       <tx:methodname="get*"propagation="REQUIRED"read-only="true"/>

                       <tx:methodname="mod*"propagation="REQUIRED"read-only="false" />         

           </tx:attributes>

   </tx:advice>

   <aop:config>  

            <aop:pointcutid="serviceMethods"expression="execution(public *com.lexing.platform.service.*.*(..))"/>

            <aop:advisor advice-ref="txAdvice"pointcut-ref="serviceMethods"/>

   </aop:config>

 

 

 

 

二.聲明式事務失效,原因

 

根本原因:由子容器掃描裝配了@Service 註解的實例。

spring的context是父子容器,由ServletContextListener 加載spring配置文件產生的是父容器,springMVC加載配置文件產生的是子容器,子容器對Controller進行掃描裝配時裝配了@Service註解的實例 (@Controller 實例依賴@Service實例),而該實例理應由父容器進行初始化以保證事務的增強處理,所以此時得到的將是原樣的Service(沒有經過事務加強處理,故而沒有事務處理能力。

 

 

三.解決辦法

1.spring配置文件applicationContext中:

 

 

 

<!-- 不掃描帶有@Controller註解的類 ,讓 springMVC 子容器加載。

 

 

<context:component-scan base-package="com.lexing.platform">  

        <context:exclude-filter   type="annotation"    expression="org.springframework.stereotype.Controller"/>  

</context:component-scan>

 

 

2.springMVC配置文件servlet-context.xml中

 

 

<!-- 將 帶有 @Service註解的類,交由spring 父容器實例化,

@Service實例依賴@Repository實例,故spring父容器也會裝配@Repository實例

-->

 

<context:component-scan base-package="com.lexing.platform">  

        <context:exclude-filter    type="annotation"   expression="org.springframework.stereotype.Service"/>  

</context:component-scan>   

 


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