1、问题

执行 entityManager.flush();  总是报错:javax.persistence.TransactionRequiredException: no transaction is in progress

字面解释是:“没有启动事务”。可是事务是在配置中交给了spring管理了,在方法上加注解 @Transactional就申明了事务的,为何说没有启动事务呢。而且配置中也启动事务注解的扫描了。 <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />。

2、原因

     折腾了很久,分析了很久,也看了一些文章,都没说在关键上,后来有一个文章豁然开朗http://blog.csdn.net/lzw190/article/details/18989301。
 

3、解决

      让springMVC的配置xml和spring容器的配置xml分开,在各自的xml中配置自己该做的事情,不要让springMVC去扫描不该自己管理的注解。

3.1  springMVC部分

3.1.1 web.xml 中 springMVC相关部分

     
   <servlet>  
        <servlet-name>springMVC</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>springMVC</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>  

3.1.2 mvc.xml 文件中关键部分

<!-- 开启springMVC注解支持 -->
     <mvc:annotation-driven />
    <mvc:default-servlet-handler/>
    
<!-- 扫描Controller -->
       <context:component-scan base-package="org.cj.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
      </context:component-scan>

3.2  spring容器部分

spring容器需要扫描的注解,放在别的xml中,让spring 的Listener去解读

3.2.1  web.xml 中spring容器关键部分

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>  
  <!--说明: 需要让spring 容器解读的配置,放在spring-开头的xml文件中 -->
          /WEB-INF/spring-*.xml  
      </param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

3.2.2 spring-core.xml文件关键部分

<!-- 自动加载构建bean -->
<context:component-scan base-package="org.cj" />

         <!--开启spring的事务申明注解支持-->
        <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

springMVC和spring各自扫描自己的注解不要相互混淆的更多相关文章

  1. SpringMVC + Spring + MyBatis 学习笔记:SpringMVC和Spring一同工作的时候,AOP事务管理不起作用的解决方法

    系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 SpringMVC 的 ...

  2. springMVC+MyBatis+Spring 整合(4) ---解决Spring MVC 对AOP不起作用的问题

    解决Spring MVC 对AOP不起作用的问题 分类: SpringMVC3x+Spring3x+MyBatis3x myibaits spring J2EE2013-11-21 11:22 640 ...

  3. SpringMVC(2)—SpringMVC整合Spring的HelloWorld

    一.这是一个SpringMVC框架的案例HelloWorld 功能:HelloWorld 二.SpringMVC运行流程 1.流程 请求-->springDispatcherServlet的ur ...

  4. Springmvc + mybatis + spring 配置,spring事物

    今天配置了半天,发现,事物不起效果,主要出现如下错误: org.mybatis.spring.transaction.SpringManagedTransaction] - [JDBC Connect ...

  5. spring + myBatis 常见错误:注解事务不回滚

    最近项目在用springMVC+spring+myBatis框架,在配置事务的时候发现一个事务不能回滚的问题. 刚开始配置如下:springMVC.xml配置内容: spring.xml配置内容 从上 ...

  6. 【SSM 1】SpringMVC、Spring和Struts的区别

    导读:近期做到的项目中,用到的框架师SSM(SpringMVC+Spring+Mybatis),那么在这之前用过SSH,这里主要是区分一下SpringMVC和Struts,但是由于SpringMVC和 ...

  7. spring mvc和spring配置扫描包问题

    spring mvc和spring俩配置文件,其中都要配置扫描包. <context:component-scan base-package="com.controller" ...

  8. springMVC用法 以及一个简单的基于springMVC hibernate spring的配置

    替代struts 1  web.xml中配置springmvc中央控制器 <?xml version="1.0" encoding="UTF-8"?> ...

  9. 菜鸟级springmvc+spring+mybatis整合开发用户登录功能(下)

    昨天介绍了mybatis与spring的整合,今天我们完成剩下的springmvc的整合工作. 要整合springmvc首先得在web.xml中配置springmvc的前端控制器DispatcherS ...

随机推荐

  1. IIS7注册本机模块

    问题描述:打开mp4文件要映射给mod_h264_streaming.dll(http://h264.code-shop.com/trac/wiki/Mod-H264-Streaming-Intern ...

  2. [转有改动]vi

    转自http://www.51testing.com/html/86/427686-247344.html 多按几次[ESC],系统会发出滴滴声以确定进入命令模式.就进入了命令模式,所有在键盘上打的字 ...

  3. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  4. 2016-2017-2 《Java程序设计》课程学生博客和代码托管链接

    2016-2017-2 <Java程序设计>课程学生博客和代码托管链接 博客 1552 20155201 李卓雯 20155202 张 旭 20155203 杜可欣 20155204 王 ...

  5. 【原】Learning Spark (Python版) 学习笔记(二)----键值对、数据读取与保存、共享特性

    本来应该上周更新的,结果碰上五一,懒癌发作,就推迟了 = =.以后还是要按时完成任务.废话不多说,第四章-第六章主要讲了三个内容:键值对.数据读取与保存与Spark的两个共享特性(累加器和广播变量). ...

  6. unsafe clr sql 部署向导

    1.部署安全权限的简单的程序集,非常简单,只需要正常发布就行,基本可以成功,前提是账号有部署clr assembly的权限 第一步: 创建登陆账号,尽量不要使用sa吧,默认架构为:dbo即可 第二部: ...

  7. Android 在非Activity的类中调用startActivityForResult

    http://www.360doc.com/content/11/0720/10/7322578_134657348.shtml

  8. github-提交仓库

    git提交仓库主要分3快 1.用命令git add告诉Git,把文件添加到本地仓库(可以用.代替提交所有) $ git add readme.txt 2.用命令git commit告诉Git,把文件提 ...

  9. SQL 事务回滚

    事务(Transaction)是并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位.通过事务,SQL Server能将逻辑相关的一组操作绑定在一起,以便服 ...

  10. 如何同时打开两个excel

    1. 打开一个excel1 2. 不要双击想要打开的excel2.右键excel应用的图标,选择excel2007. 3. 将excel2拖动到2所打开的新建excel中. 4. over.