编程事务之基于注解的声明式事务控制

x33g5p2x  于2022-08-17 转载在 其他  
字(3.0k)|赞(0)|评价(0)|浏览(902)

基于注解的声明式事务控制

使用注解方式(改动bean,自定义的bean用注解,非自定义的bean配置到配置文件中去),需要改动两个,一个是配置文件applicationContext,另一个是AccountServiceImpl类

  1. package com_1.service.impl;
  2. import com.dao.AccountDao;
  3. import com.service.AccountService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.transaction.annotation.Isolation;
  7. import org.springframework.transaction.annotation.Propagation;
  8. import org.springframework.transaction.annotation.Transactional;
  9. @Service("accountService")
  10. //在类上使用事务,其中的所有方法都会生效
  11. @Transactional(isolation = Isolation.REPEATABLE_READ)
  12. public class AccountServiceImpl implements AccountService {
  13. @Autowired
  14. private AccountDao accountDao;
  15. @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
  16. public void transfer(String outMan, String inMan, double money) {
  17. accountDao.out(outMan,money);
  18. accountDao.in(inMan,money);
  19. }
  20. }

applicationContext配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
  11. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  12. ">
  13. <!--组件扫描,才能扫到@Service注解-->
  14. <context:component-scan base-package="com_1"/>
  15. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  16. <property name="driverClass" value="com.mysql.jdbc.Driver"/>
  17. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
  18. <property name="user" value="root"/>
  19. <property name="password" value="123456"/>
  20. </bean>
  21. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  22. <property name="dataSource" ref="dataSource"/>
  23. </bean>
  24. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  25. <property name="dataSource" ref="dataSource"/>
  26. </bean>
  27. <!--事务的注解驱动,不写就没开启事务-->
  28. <tx:annotation-driven transaction-manager="transactionManager"/>
  29. </beans>

运行结果

 注解配置声明式事务控制解析

①使用@Transactional在需要进行事务控制的类或是方法上修饰,注解可用的属性同xml配置方式,例如隔离级别、传播行为等。

②注解使用在类上,那么该类下的所有方法都使用同一套注解参数配置

③使用在方法上,不同的方法可以采用不同的事务参数配置。

④xml配置文件中要开启事务的注解驱动tx:annotation-driven/

配置要点

平台事务管理器配置(xml方式)

事务通知的配置(@Transactional注解)

事务注解驱动的配置tx:annotation-driven/

相关文章