Spring MVC JdbcTemplate 事务性注释不起作用

Spring MVC JdbcTemplate Transactional Annotation does not work

我在一个 http post 请求中有两个插入操作。像

@Service
public class StudentService {

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public int create(final Student student) {
        // insert into table 1

        // insert into table 2 using id return from insert 1, but something BAD happen here
    }
}

所以,我在方法级别添加了 @Transactional,如上所示。

为了让它工作,我添加了这个tx:annotation-driven

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

所以我的 app-servlet.xml 看起来像

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <mvc:annotation-driven />
    <mvc:resources mapping="/res/**" location="/res/" />

    <context:component-scan base-package="com.app" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="dataSource" name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://xxx:3306/AppDb" />
        <property name="username" value="username" />
        <property name="password" value="password" />
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</beans>

component-scan 将基本上扫描 所有 class 包括 controller, service, model, viewmodel, helper class 等

如果我删除 tx:annotation-driven@transactional 将不起作用。 但是一旦我添加这个 tx:annotation-driven,就会出现一些错误,比如

Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.app.service.StudentService com.app.controller.HomeController.studentRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentService' defined in file [/path/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/MathSchool/WEB-INF/classes/com/app/service/StudentService.class]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/cglib/transform/impl/MemorySafeUndeclaredThrowableStrategy

我怎样才能让这个 @transactional 工作。

感谢@Jens

我的项目使用 spring 4.1.6spring security 3.2.7

根据此文档Migrating from Spring Security 3.x to 4.x (XML Configuration)

Spring Security 4 now requires Spring 4 (Spring 4.0 or 4.1, I guess). Conveniently, Spring Security 3.2.x works with Spring 3.2.x and Spring 4 (Spring 4.0 Only, I guess).

我唯一做的就是将 Spring 4.1 降级为 Spring 4.0