Spring MVC JdbcTemplate事务注释不起作用

永恒的

我在一个http发布请求中有两个插入操作。就像是

@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将基本扫描所有类包括controllerservicemodelviewmodelhelper类等等。

如果我删除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

根据本文档,从Spring Security 3.x迁移到4.x(XML配置)

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.1Spring 4.0

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

@Size和@Pattern注释在Spring MVC中不起作用

来自分类Dev

基于Spring MVC注释的配置不起作用

来自分类Dev

Spring Mvc 测试 webapp @Autowired 注释不起作用

来自分类Dev

事务注释在Spring Boot中不起作用

来自分类Dev

Spring MVC表单验证不起作用

来自分类Dev

Spring MVC请求映射不起作用

来自分类Dev

Spring MVC验证不起作用

来自分类Dev

Spring MVC,表单提交不起作用

来自分类Dev

@Range在Spring MVC中不起作用

来自分类Dev

SwaggerSpringMvcPlugin在Spring MVC上不起作用

来自分类Dev

OpenEntityManagerInViewFilter不起作用-Spring MVC

来自分类Dev

Spring MVC,表单提交不起作用

来自分类Dev

Spring MVC资源不起作用

来自分类Dev

Spring MVC 4 ResourceHandlers不起作用

来自分类Dev

Spring MVC @Pathvariable 不起作用

来自分类Dev

注销在 spring mvc 中不起作用

来自分类Dev

Spring MVC国际化不起作用

来自分类Dev

Spring MVC语言环境更改不起作用

来自分类Dev

Spring MVC 4服务@tTransactional不起作用

来自分类Dev

表单验证在Spring Mvc Application中不起作用

来自分类Dev

Spring MVC Custom Converter for JSON数据不起作用

来自分类Dev

Spring MVC页面到页面导航不起作用

来自分类Dev

Spring MVC 4服务@tTransactional不起作用

来自分类Dev

按名称自动接线在Spring MVC中不起作用

来自分类Dev

Spring MVC调度程序不起作用。HTTP 404

来自分类Dev

Spring MVC基本登录页面功能不起作用

来自分类Dev

STS:使用 Spring MVC 提供 Web 内容不起作用

来自分类Dev

Spring MVC/Simple Post 请求不起作用

来自分类Dev

POST 方法在 Spring MVC 中不起作用