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 {
}
com.yourname.config.DataSourceConfiguration
/**
* @author gregPerlinLi
* @since 2021-09-28
*/
@PropertySource("classpath:jdbc.properties")
@Configuration
public class DataSourceConfiguration {
@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(value = "dataSource")
public DataSource dataSource() throws Exception {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean(value = "jdbcTemplate")
public JdbcTemplate jdbcTemplate() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
return jdbcTemplate;
}
@Bean(value = "transactionManager")
public PlatformTransactionManager transactionManager(DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
}
com.yourname.dao.impl.AccountDao
/**
* @author gregPerlinLi
* @since 2021-09-28
*/
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void out(String outMan, BigDecimal money) {
jdbcTemplate.update("update account set money = money - ? where name = ?", money, outMan);
}
@Override
public void in(String inMan, BigDecimal money) {
jdbcTemplate.update("update account set money = money + ? where name = ?", money, inMan);
}
}
com.yourname.service.impl.AccountService
/**
* @author gregPerlinLi
* @since 2021-09-28
*/
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Override
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
public void transfer(String outMan, String inMan, BigDecimal money) {
accountDao.out(outMan, money);
accountDao.in(inMan, money);
}
}
com.yourname.controller.AccountController
/**
* @author gregPerlinLi
* @since 2021-09-28
*/
public class AccountController {
public static void main(String[] args) {
ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class);
AccountService accountService = app.getBean(AccountService.class);
accountService.transfer("Tom", "Lucy", BigDecimal.valueOf(100));
}
}
2. 注解配置声明式事务控制解析
- 使用
@Transactional
在需要进行事务控制的类或者是方法上修饰,注解要用的属性同 XML 的配置方式,如隔离界别、传播行为等。 - 注解使用在类上,那么该类下的所有方法都使用同一套注解配置。
- 使用方法上,不同的方法可以采用不同的事务参数配置
- 需要在
SpringConfiguration
中开启事务的注解驱动@EnableTransactionManagement
3. 知识要点
注解声明式事务控制配置要点
- 平台事务管理器配置(注解方式)
- 事务通知的配置(
@Transaction
注解配置) - 事务注解驱动的配置
@EnableTransactionManagement