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中写上事物处理的代码(本人尚未测试哦 只是思路)

 

posted @   知行IT讲堂  阅读(4089)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
阅读排行:
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· DeepSeek本地性能调优
· 一文掌握DeepSeek本地部署+Page Assist浏览器插件+C#接口调用+局域网访问!全攻略
历史上的今天:
2016-09-05 android 之 webView 显示h5 执行选择图片或者拍照功能
点击右上角即可分享
微信分享提示