springbootプロジェクトでは、@ EnableTransactionManagementアノテーションを使用する必要はありません。@ Transactionalアノテーションをビジネスメソッドに直接追加できますが、純粋なspringmvcプロジェクトの場合は、@ EnableTransactionManagementアノテーションを構成クラスに追加する必要があります。グローバルトランザクションを開始します
実際、理由は非常に単純です。なぜなら、springbootプロジェクトは、開始時に@EnableTransactionManagementアノテーションを自動的に追加したからです。
jarパッケージspring-boot-autoconfigure-2.2.2.RELEASE.jarには、自動的に注入されるBeanが定義されています。
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
ただし、このBeanを注入するための前提条件があります。TransactionAutoConfigurationは、BeanDataSourceTransactionManagerAutoConfigurationが注入された後に注入する必要があります。
TransactionAutoConfiguration Beanでは、静的内部クラスが宣言されています。EnableTransactionManagementConfiguration
そのクラスでは、トランザクションアノテーションを有効にするコメントが宣言されています。
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnBean({
TransactionManager.class})
@ConditionalOnMissingBean({
AbstractTransactionManagementConfiguration.class})
public static class EnableTransactionManagementConfiguration {
public EnableTransactionManagementConfiguration() {
}
@Configuration(
proxyBeanMethods = false
)
@EnableTransactionManagement(
proxyTargetClass = true
)
@ConditionalOnProperty(
prefix = "spring.aop",
name = {
"proxy-target-class"},
havingValue = "true",
matchIfMissing = true
)
public static class CglibAutoProxyConfiguration {
public CglibAutoProxyConfiguration() {
}
}
@Configuration(
proxyBeanMethods = false
)
@EnableTransactionManagement(
proxyTargetClass = false
)
@ConditionalOnProperty(
prefix = "spring.aop",
name = {
"proxy-target-class"},
havingValue = "false",
matchIfMissing = false
)
public static class JdkDynamicAutoProxyConfiguration {
public JdkDynamicAutoProxyConfiguration() {
}
}
}