Springmvc controller 层 @Transactional 不起作用

Springmvc controller 层 @Transactional 不起作用

这个主要是扫描惹得祸,

这个是正常编程模式,有Service扫描的配置(网上搜索全都是这种的)

Spring_MVC.xml

<context:component-scan base-package="com.pamirs.pradar.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
    </context:component-scan>


applicationContext.xml

<context:component-scan base-package="com.pamirs.pradar.controller">  
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

很尴尬发现我根本没有写Service层,程序也写的差不多了,不想添加多一层太麻烦了。网上搜索基本都是上面那一种,有些人还说controller层不可以用@Transactional注解,我还真的信了...。
后面用了手动事务,才觉得不可能呢,都是一样的怎么不行呢,重新尝试了一下竟然好了。

这个是我后面的配置

applicationContext.xml

<bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>
	<context:component-scan base-package="com.*">
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan> 

Spring_MVC.xml

<context:component-scan base-package="com.lijianjun"> 
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<tx:annotation-driven transaction-manager="transactionManager"/>

将Spring的配置controller的扫描关掉了,让Spring-MVC自己去扫描自己的controller.

@transanction解释器的部分,将@transanction解释器在Spring-mvc中开启,这样就ok了。

       自己在想了一下实际上就是Spring与Spring mvc配置文件解析的是分开的,你在applicationContext.xml里面开启了@transanction解释器,就会在applicationContext.xml配置的扫描包的时候把扫描到的@transanction这样的注解开启事务,然后Spring-mvc.xml也有扫描而且没有开启解释器就把,有事务功能的controller替换为没有事务功能的controller,@Transactional 就不起作用


评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尘锿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值