【重要】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>   

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值