我在一个 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
基本上会扫描所有类,包括controller
类、service
类、model
类、viewmodel
类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
工作。
感谢@Jens
我使用spring 4.1.6
和spring security 3.2.7
的项目
根据此文档从 Spring Security 3.x 迁移到 4.x(XML 配置)
SpringSecurity 4 现在需要 Spring 4(我猜是 Spring 4.0 或 4.1)。方便,弹簧安全 3.2.x适用于Spring 3.2.x和Spring 4(我猜只有Spring 4.0)。
我唯一做的就是将Spring 4.1
降级为Spring 4.0