Spring-tx-@EnableTransactionManagement注解

本文介绍了Spring中@EnableTransactionManagement注解在配置事务管理中的作用,以及它与XML配置文件中tx标签的对应关系。通过实例展示了如何在Java配置类中启用事务,并强调了@Configuration和@EnableTransactionManagement的配合使用关键。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

xml形式的spring配置使用tx标签来开启事务
而注解形式的spring配置,则使用@EnableTransactionManagement注解

其实本文要说的内容,在该注解的注释当中都有,例如下面这个代码

@Component
public class Creater1 {

	@Autowired
	JdbcTemplate jdbcTemplate;

	@Transactional(rollbackFor = Exception.class)
	public void create() {
		jdbcTemplate.update("insert into t1(value) values ('HAHA')");
		throw new RuntimeException("抛异常就回滚");
	}
}
import javax.sql.DataSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
@EnableTransactionManagement
public class TestMain {

	@Bean
	public DataSource dataSource() {
		HikariDataSource ds = new HikariDataSource();
		ds.setJdbcUrl("jdbc:mysql://localhost:3306/swttest?serverTimezone=UTC&&useSSL=false");
		ds.setUsername("root");
		ds.setPassword("123456");
		return ds;
	}

	@Bean
	public JdbcTemplate jdbcTemplate() {
		JdbcTemplate jdbc = new JdbcTemplate();
		jdbc.setDataSource(dataSource());
		return jdbc;
	}

	@Bean
	public PlatformTransactionManager platformTransactionManager() {
		return new DataSourceTransactionManager(dataSource());
	}

	public static void main(String[] args) throws Throwable {
		// 依赖容器的话必须要开启@EnableTransactionManagement注解,表示告诉spring我要使用事务
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
				"com.example.studyintegration.spring.Transcation.TranscationStatus");
		Creater1 tt = ctx.getBean(Creater1.class);
		tt.create();
	}
}

上面就是使用该注解的方式,开启事务,抛异常就会回滚,需要注意的地方:
@EnableTransactionManagement注解必须配合@Configuration注解使用,否则不会生效事务功能

日记:@Configuration注解等同于java版本的xml文件,以前xml版本的tx标签是写到xml文件中的,而EnableTransactionManagement等同于tx标签,这也更加印证了Configuration注解就是一个java抽象的xml文件

在@Configuration的注释中其中就说明了这一项

Enabling built-in Spring features using @Enable annotations
Spring features such as asynchronous method execution, 
scheduled task execution,
annotation driven transaction management, 
and even Spring MVC can be enabled andconfigured from @Configuration classes using their respective "@Enable"annotations. 
See @EnableAsync, 
@EnableScheduling, 
@EnableTransactionManagement,
@EnableAspectJAutoProxy,
and @EnableWebMvcfor details.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值