zoukankan      html  css  js  c++  java
  • 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中写上事物处理的代码(本人尚未测试哦 只是思路)
    
    
    
  • 相关阅读:
    centos-docker安装及基本使用
    List集合-03.Vector
    List集合-02.LinkedList
    List集合-01.ArrayList
    常用图像处理方法
    git使用
    bug记录
    bash vim等常用命令
    python常用
    MTCNN试用
  • 原文地址:https://www.cnblogs.com/nmdzwps/p/9593137.html
Copyright © 2011-2022 走看看