2015-04-17 93 views
0

我在一個http post請求中有兩個插入操作。像Spring MVC JdbcTemplate Transactional Annotation不起作用

@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,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 

我怎樣才能讓這個@transactional工作。

+0

的可能重複[如何解決與Tomcat的,春天和Maven這個奇怪的錯誤?(http://stackoverflow.com/questions/18361136/如何解決這個奇怪的錯誤與tomcat春天和maven) – Jens

+0

@Jens我有春季核心瓶包括在內。 – Timeless

+0

但是什麼版本? – Jens

回答

0

由於@Jens

我的項目中使用spring 4.1.6spring security 3.2.7

根據這一文件Migrating from Spring Security 3.x to 4.x (XML Configuration)

春季安全4,現在需要彈簧4(春季4.0或4.1,我猜)。 Spring Security 3.2.x適用於Spring 3.2.x和Spring 4(我猜是Spring 4.0)。

我做的唯一的事情就是降級Spring 4.1Spring 4.0