Spring可以支持编程式事务和声明式事务。
Spring使用事务管理器,每个不同平台的事务管理器都实现了接口:PlatformTransactionManager
此接口是事务管理的核心,提供了三个需要实现的函数:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="accountService" class="com.lc.service.impl.AccountServiceImple">
<property name="accountDao" ref="accountDao">property>
bean>
<bean id="accountDao" class="com.lc.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource">property>
bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://localhost:3306/eesy">property>
<property name="username" value="root">property>
<property name="password" value="kwantler">property>
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">property>
bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut id="pt1" expression="execution(* com.lc.service.impl.*.*(..))">aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1">aop:advisor>
aop:config>
beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/tx/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.lc">context:component-scan>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource">property>
bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://localhost:3306/eesy">property>
<property name="username" value="root">property>
<property name="password" value="kwantler">property>
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">property>
bean>
<tx:annotation-driven transaction-manager="transactionManager">tx:annotation-driven>
beans>
package config;
import org.springframework.context.annotation.*;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* 该类是一个配置类,相遇bean.xml一样
* spring中的新注解
* @Configuration 指当前类是一个配置类
* 细节:当配置对象是AnnotationConfigApplicationContext(SpringConfiguration.class)的参数时可以不写
* @ComponentScan 用于通过注解指定spring在创建容器时要扫描的包
* 属性:value 他和basePackages作用一样都是用来指定创建容器时要扫描的包
* 等同于在xml中配置了
*
* @Bean 将当前方法的返回值作为bean对象存入spring的容器中
* 属性:name ->用来指定bean的ID,默认是当前方法名称
* 细节:当我们用注解来配置的话,如果有参数,spring会去容器中查找是否有可用的参数
* @import(JdbcConfig.class) 用于置指定其他配置类
* 属性:value用来指定其他配置字节码,有import的类就是主(父)配置类,导入的都是子配置类
* @PropertySource("classpath:jdbcConfig.properties") 用于指定properties文件的路径:classpath 表示类路径下
* @EnableTransactionManagement 开启spring对注解事物的支持
* 相当于
*/
@Configuration
@ComponentScan("com.lc")
@Import({JdbcConfig.class, TransactionConfig.class})
@PropertySource("classpath:jdbcConfig.properties")
@EnableTransactionManagement
public class SpringConfiguration {
}
package config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* @Bean 将当前方法的返回值作为bean对象存入spring的容器中
* 用于创建一个QueryRunner 对象
* @Scope("prototype") 注明是多例
* @param dataSource
* @return
*/
@Bean(name="runner")
@Scope("prototype")
public JdbcTemplate createJdbcTemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}
/**
* 创建数据源对象
* @return
*/
@Bean(name="dataSource")
public DataSource createDataSource(){
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
}
package config;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
public class TransactionConfig {
/**
* 配置事务管理器对象注入
* @param dataSource
* @return
*/
@Bean(name="transactionManager")
public PlatformTransactionManager createTrasactionManger(DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
}
**
* todo junit 单元测试
* Spring 整合junit的配置要求junite4.1.2 以上版本
* 1.导入Spring整合Junit的jar
* 2.告知spring运行器,spring和ioc创建是基于xml还是注解的,并且说明位置
* @contextConfiguration locations:指定xml文件的位置:加上classpath表示在当前类路径下
* classes: 指定注解类所在位置
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {
@Autowired
private AccountService accountService;
@Test
public void transferAccount(){
accountService.transfer("bbb", "ccc", 2F);
}
@Test
public void transferTest() {
}
}
编程式事务管理:这个大神写的很详细
首先: 因为我们使用的是特定的平台,所以,我们需要创建一个合适我们的平台事务管理PlateformTransactionManager。如果使用的是JDBC的话,就用DataSourceTransactionManager。注意需要传入一个DataSource,这样,平台才知道如何和数据库打交道。
第二: 为了使得平台事务管理器对我们来说是透明的,就需要使用 TransactionTemplate。使用TransactionTemplat需要传入一个 PlateformTransactionManager 进入,这样,我们就得到了一个 TransactionTemplate,而不用关心到底使用的是什么平台了。
第三: TransactionTemplate 的重要方法就是 execute 方法,此方法就是调用 TransactionCallback 进行处理。
也就是说,实际上我们需要处理的事情全部都是在 TransactionCallback 中编码的。
第四: 也就是 TransactionCallback 接口,我们可以定义一个类并实现此接口,然后作为 TransactionTemplate.execute 的参数。把需要完成的事情放到 doInTransaction中,并且传入一个 TransactionStatus 参数。此参数是来调用回滚的。
也就是说 ,PlateformTransactionManager 和 TransactionTemplate 只需在程序中定义一次,而TransactionCallback 和 TransactionStatus 就要针对不同的任务多次定义了。
这就是Spring的编程式事务管理。