我在一个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
将基本扫描所有类包括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配置)
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句