基于注解的声明式事务控制
注意:
- 使用@Transcational在需要进行实物控制的类或是方法上修饰,注解可用的属性xml配置方式,列如:隔离级别,传播行为等
- 注解可以写在类上也可以写在方法上,方法上的优先级高于类的
- 一定要加上实物注解驱动tx:annotation-driven
配置要点
- 平台事务管理器配置(xml方式)
- 事务通知配置(@Transcational)
- 事务注解驱动配置(< tx:annotation-driver >)
示例
@ComponentScan
@Configuration
public class BeanConfig {
@Bean("jdbcTemplate")
public JdbcTemplate returnJdbcTemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Bean("dataSourceTransactionManager")
public DataSourceTransactionManager returnDataSourceTransactionManager(DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Configuration
@PropertySource("classpath:c3p0config.properties")
public class MysqlConfig {
@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("dataSource")
public DataSource c3p0DataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource;
}
}
@Configuration
@Import({BeanConfig.class,MysqlConfig.class})
@ComponentScan("com.itheima")
public class SpringConfig {
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Account {
private String name;
private double money;
}
@Service("accountService")
@EnableTransactionManagement
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Override
@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,timeout = -1,readOnly = false)
public void transfer(String outMan, String inMan, double money) {
System.out.println("accountDao=============="+accountDao);
accountDao.out(outMan,money);
int i = 1/0;
accountDao.in(inMan,money);
}
}
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void out(String outMan, double money) {
jdbcTemplate.update("update account set money = money - ? where name = ?",money,outMan);
}
@Override
public void in(String inMan, double money) {
jdbcTemplate.update("update account set money = money + ? where name = ?",money,inMan);
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class AccountServiceImplTest {
@Autowired
private AccountService accountService;
@Test
public void transferTest() {
accountService.transfer("tom","lucy",1000);
}
}
@EnableTransactionManagement和@Transcational注解
- @EnableTransactionManagement注解
- 这个注解表示支持事务管理器,必须标注在类上
- 使用此注解一定要有DataSourceTransactionManager类的Bean对象(不同Dao层事务支持的实现类是不同的),否则会报错,找不到事务管理器的Bean对象
- 这个注解也相当于开启了事务注解的驱动
- @Transcational注解
- 相当于是一个事务的属性注解
- 可以标注在类上,也可以标注在方法上,在配置文件中使用的事务属性在注解中也同样适用;在方法上的同样属性的优先级高于在类上的优先级
注意:
- 在测试类时,一定要添加一个总的配置类,否则会一致报错,失败加载了Application.xml文件
- 在测试时,如果使用自动注入的话,一定要添加@RunWith(SpringJUnit4ClassRunner.class)这个注解