spring mvc 把事物处理配置到Controller层

本来事物处理是要配置到service的,无奈项目是这样的,来到新公司接手的项目是多个项目用的公共的service,为了不在service中不添加不是公用的方法,每个项目用到的方法都写在了controller层,现在呢要给一些多表操作的方法添加事物处理,本来是打算把controller层的方法挪到service,但是这样的公用的service就会会添加很多方法,而另一个项目就用不到,或者另一个项目用到的方法这个项目用不到。最终决定 如果事物能加在controller层 就可以解决现在的问题了。

具体做法如下 只罗列关键配置

在spring.xml中配置(就是sprign的配置文件)
<!-- 扫描service、dao -->
<!-- 扫描注解@Component , @Service , @Repository。 要把 controller去除,controller是在spring-servlet.xml中配置的,如果不去除会影响事务管理的。 -->

<!-- 扫描service、dao -->

<!-- 扫描注解@Component , @Service , @Repository。 要把 controller去除,controller是在spring-servlet.xml中配置的,如果不去除会影响事务管理的。 -->
<context:component-scan base-package="com.systek.scenic">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
 </context:component-scan>
<!-- 或者如下写法也行 include是包含  exclude是排除  任意注释掉一个或者都不注释都可以的-->
<!--<context:component-scan base-package="com.systek.scenic">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>-->

在mvc.xml中配置(就是spring Mvc的配置文件)

 <!-- 注解扫描包 -->
    <context:component-scan base-package="com.systek.scenic.web.controller"/>
 <!-- 开启注解 -->
    <mvc:annotation-driven/>
    <!-- 开启注解 -->
    <import resource="classpath:spring-tx.xml"/>

 

在事物处理spring-tx.xml的配置文件中配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd  
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd">


 <!-- 事务配置 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dbPool" />
    </bean>
    <!-- 使用annotation注解方式配置事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

 

然后在>controller层中对应的方法上加上@Transactional注解即可 如

 @RequestMapping(value = "/Save", method = RequestMethod.POST)
    @ResponseBody
   //@Transactional(rollbackFor = { Exception.class })
    @Transactional
    public Result save(HttpServletRequest request, GuiderRentVO guiderRentVO, Model model)
    {
//......
}

 

经测试注解@Transactional写在controller是管用的 写在service是不管用的,因为我们的事务处理代码写在了mvc.xml中
如果需要在service也支持 可以尝试在spring.xml中写上事物处理的代码(本人尚未测试哦 只是思路)
 
 

 

转载于:https://www.cnblogs.com/nmdzwps/p/9593137.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值