Spring MVC JdbcTemplate Transactional Annotation不起作用

时间:2015-04-17 09:46:58

标签: spring spring-mvc spring-transactions

我在一个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基本上会扫描所有类,包括controllerservicemodelviewmodel,{{1} }等等。

如果我删除helper,则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 工作。

1 个答案:

答案 0 :(得分:0)

感谢@Jens

我的项目使用spring 4.1.6spring security 3.2.7

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

  

Spring Security 4现在需要Spring 4(我想是Spring 4.0或4.1)。方便的是,Spring Security   3.2.x适用于Spring 3.2.x和Spring 4(仅限Spring 4.0)。

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