声明式事务(纯注解驱动)**
-
名称:@EnableTransactionManagement
-
纯注解驱动 最主要的注解,写在核心配置类上
-
类型:类注解
-
位置:Spring注解配置类上方
-
作用:开启注解驱动,等同XML格式中的注解驱动
-
范例:
@Configuration @ComponentScan("com.itheima") @PropertySource("classpath:jdbc.properties") @Import({JDBCConfig.class,MyBatisConfig.class,TransactionManagerConfig.class}) @EnableTransactionManagement public class SpringConfig { }
public class TransactionManagerConfig { @Bean public PlatformTransactionManager getTransactionManager(@Autowired DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } }
@Autowired 自动注入注解 也可以在参数是 对象的情况下,在参数里使用的。
PlatformTransactionManager 这个是spring中平台事务管理对象
作用是:开启事务 提交事务 回滚事务 关闭事务。
它需要 DataSource这个资源对象 作为参数才能创建出平台事务管理对象