Spring Boot 中的 @EnableTransactionManagement

Spring Boot 中的 @EnableTransactionManagement

问题描述:

Spring Boot 需要 @EnableTransactionManagement 吗?我做了一些研究.有些人说你不需要它,因为 Spring Boot 已经启用了它,其他人说你必须明确使用它.那么怎么样?

Is @EnableTransactionManagement required in Spring Boot? I did some research. Some folks say you don't need it, as Spring Boot has it already enabled, others say you do have to use it explicitly. So how is it?

可能您也在使用 Spring Data.默认情况下,即使没有 @EnableTransactionManagement,对 Spring Data 存储库的调用也被事务包围.如果 Spring Data 找到一个现有的事务,则现有的事务将被重用,否则会创建一个新的事务.

Probably you're also using Spring Data. Calls on Spring Data repositories are by default surrounded by a transaction, even without @EnableTransactionManagement. If Spring Data finds an existing transaction, the existing transaction will be re-used, otherwise a new transaction is created.

@Transactional 注释仅在您激活 @EnableTransactionManagement(或以其他方式配置事务处理)时才进行评估.

@Transactional annotations within your own code, however, are only evaluated when you have @EnableTransactionManagement activated (or configured transaction handling some other way).

您可以通过将以下属性添加到您的 application.properties 来轻松跟踪事务行为:

You can easily trace transaction behavior by adding the following property to your application.properties:

logging.level.org.springframework.transaction.interceptor=TRACE

(参见在日志中显示 Spring 事务)