springmvc spring 事务不回滚

来源:互联网 发布:启示录2知乎 编辑:程序博客网 时间:2024/06/03 23:02

Spring容器优先加载由ServletContextListener(对应applicationContext.xml)产生的父容器,而SpringMVC(对应mvc_dispatcher_servlet.xml)产生的是子容器。子容器Controller进行扫描装配时装配的@Service注解的实例是没有经过事务加强处理,即没有事务处理能力的Service,而父容器进行初始化的Service是保证事务的增强处理能力的。如果不在子容器中将Service exclude掉,此时得到的将是原样的无事务处理能力的Service,因为在多上下文的情况下,如果同一个bean被定义两次,后面一个优先。

在context-dispatcher.xml 中 不扫描service:

<context:component-scan base-package="*">        <context:include-filter type="annotation"            expression="org.springframework.stereotype.Controller" />        <context:exclude-filter type="annotation"            expression="org.springframework.stereotype.Service" />    </context:component-scan>

在applicationContext.xml 添加扫描service:

<context:component-scan base-package="service" />

注意:
service中不能用try catch把异常捕获,否则事务不回滚。
可以抛出异常

throw new RuntimeException();
原创粉丝点击