---
title: "使用springmvc后事物不起作用的原因"
author: "zzq19860626"
date: "2015-12-25T23:41:28Z"
tags: ["Spring", "事物"]
categories: ["编程语言"]
slug: "springmvc"
draft: false
lastmod: "2015-12-25T23:41:28Z"
id: 2266701
url: "https://alaric.iteye.com/blog/2266701"
---

问题表现：用springmvc后事物不起作用  
解决方法：  
1、查所有关于事物的配置都没有问题；  
2、查找日志，发现配置@postConstruct注解的方法在服务启动过程中执行了两次；  
3、为什么出现两次，百度后知道在springmvc的注解扫描配置中要用&lt;context:exclude-filter&gt;来过滤排除掉@service，如果不排除就会初始化两次servicebean，导致事物失效。如果说spring 容器 初始化的时候service注解配置的事物能正常使用，那么springmvc的扫描service初始化事物也应该成功，为什么失败呢？原因是事物管理器的配置是以xml的配置方式配在applicationContext中的，而在springmvc的配置中并没有配置，所以导致事物两次初始化servicebean后不起作用。

```
   <context:component-scan base-package="com.taotaosou" >
     <context:exclude-filter type="annotation"  expression="org.springframework.stereotype.Service" />
    </context:component-scan>
```