SpringMVC 第二十一章 基于注解的声明式事务控制


SpringMVC —— 第二十一章 基于注解的声明式事务控制


1. 快速入门

示例代码:

com.yourname.config.SpringConfiguration

/**
 * <code>@EnableTransactionManagement</code>: Enable Transaction Management
 * @author gregPerlinLi
 * @since 2021-09-16
 */
@ComponentScan("com.gregperlinli.*")
@Import(DataSourceConfiguration.class)
@EnableTransactionManagement
@Configuration
public class SpringConfiguration &#123;
&#125;

com.yourname.config.DataSourceConfiguration

/**
 * @author gregPerlinLi
 * @since 2021-09-28
 */
@PropertySource("classpath:jdbc.properties")
@Configuration
public class DataSourceConfiguration &#123;
    @Value("$&#123;jdbc.driver&#125;")
    private String driver;
    @Value("$&#123;jdbc.url&#125;")
    private String url;
    @Value("$&#123;jdbc.username&#125;")
    private String username;
    @Value("$&#123;jdbc.password&#125;")
    private String password;
      
    @Bean(value = "dataSource")
    public DataSource dataSource() throws Exception &#123;
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        return dataSource;
    &#125;
    @Bean(value = "jdbcTemplate")
    public JdbcTemplate jdbcTemplate() throws Exception &#123;
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
        return  jdbcTemplate;
    &#125;
    @Bean(value = "transactionManager")
    public PlatformTransactionManager transactionManager(DataSource dataSource)&#123;
        return  new DataSourceTransactionManager(dataSource);
    &#125;
&#125;

com.yourname.dao.impl.AccountDao

/**
 * @author gregPerlinLi
 * @since 2021-09-28
 */
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao &#123;
    @Autowired
    private JdbcTemplate jdbcTemplate;
  
    @Override
    public void out(String outMan, BigDecimal money) &#123;
        jdbcTemplate.update("update account set money = money - ? where name = ?", money, outMan);
    &#125;
    @Override
    public void in(String inMan, BigDecimal money) &#123;
        jdbcTemplate.update("update account set money = money + ? where name = ?", money, inMan);
    &#125;
&#125;

com.yourname.service.impl.AccountService

/**
 * @author gregPerlinLi
 * @since 2021-09-28
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService &#123;
    @Autowired
    private AccountDao accountDao;

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void transfer(String outMan, String inMan, BigDecimal money) &#123;
        accountDao.out(outMan, money);
        accountDao.in(inMan, money);
    &#125;
&#125;

com.yourname.controller.AccountController

/**
 * @author gregPerlinLi
 * @since 2021-09-28
 */
public class AccountController &#123;
    public static void main(String[] args) &#123;
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        AccountService accountService = app.getBean(AccountService.class);
        accountService.transfer("Tom", "Lucy", BigDecimal.valueOf(100));
    &#125;
&#125;

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

  1. 使用 @Transactional 在需要进行事务控制的类或者是方法上修饰,注解要用的属性同 XML 的配置方式,如隔离界别、传播行为等。
  2. 注解使用在类上,那么该类下的所有方法都使用同一套注解配置。
  3. 使用方法上,不同的方法可以采用不同的事务参数配置
  4. 需要在 SpringConfiguration 中开启事务的注解驱动 @EnableTransactionManagement

3. 知识要点

注解声明式事务控制配置要点

  • 平台事务管理器配置(注解方式)
  • 事务通知的配置(@Transaction 注解配置)
  • 事务注解驱动的配置 @EnableTransactionManagement


文章作者: gregPerlinLi
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 gregPerlinLi !
  目录