From bfd1a29e53a8f2afe1fe569cf25921bd8c2b49c4 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 29 Sep 2023 14:11:43 +0100 Subject: [PATCH] Exclude AbstractTransactionAspect from lazy init Fixes gh-37506 --- .../spring-boot-autoconfigure/build.gradle | 3 ++- .../TransactionAutoConfiguration.java | 13 ++++++++++++ .../TransactionAutoConfigurationTests.java | 21 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/build.gradle b/spring-boot-project/spring-boot-autoconfigure/build.gradle index 724c0f502e..0caebc41ed 100644 --- a/spring-boot-project/spring-boot-autoconfigure/build.gradle +++ b/spring-boot-project/spring-boot-autoconfigure/build.gradle @@ -156,11 +156,12 @@ dependencies { optional("org.mongodb:mongodb-driver-reactivestreams") optional("org.mongodb:mongodb-driver-sync") optional("org.quartz-scheduler:quartz") - optional("org.springframework:spring-jdbc") optional("org.springframework.integration:spring-integration-core") optional("org.springframework.integration:spring-integration-jdbc") optional("org.springframework.integration:spring-integration-jmx") optional("org.springframework.integration:spring-integration-rsocket") + optional("org.springframework:spring-aspects") + optional("org.springframework:spring-jdbc") optional("org.springframework:spring-jms") optional("org.springframework:spring-orm") optional("org.springframework:spring-tx") diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionAutoConfiguration.java index 56ee075564..f359612b90 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionAutoConfiguration.java @@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.transaction; import java.util.stream.Collectors; import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.LazyInitializationExcludeFilter; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -33,6 +34,7 @@ import org.springframework.transaction.ReactiveTransactionManager; import org.springframework.transaction.TransactionManager; import org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration; import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.springframework.transaction.aspectj.AbstractTransactionAspect; import org.springframework.transaction.reactive.TransactionalOperator; import org.springframework.transaction.support.TransactionOperations; import org.springframework.transaction.support.TransactionTemplate; @@ -97,4 +99,15 @@ public class TransactionAutoConfiguration { } + @Configuration(proxyBeanMethods = false) + @ConditionalOnBean(AbstractTransactionAspect.class) + static class AspectJTransactionManagementConfiguration { + + @Bean + static LazyInitializationExcludeFilter eagerTransactionAspect() { + return LazyInitializationExcludeFilter.forBeanTypes(AbstractTransactionAspect.class); + } + + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/TransactionAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/TransactionAutoConfigurationTests.java index 502124eb17..f8f17332db 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/TransactionAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/TransactionAutoConfigurationTests.java @@ -22,11 +22,15 @@ import javax.sql.DataSource; import org.junit.jupiter.api.Test; +import org.springframework.boot.LazyInitializationExcludeFilter; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; +import org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener; import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.boot.logging.LogLevel; import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.context.annotation.AdviceMode; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -35,6 +39,7 @@ import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.ReactiveTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.aspectj.AbstractTransactionAspect; import org.springframework.transaction.reactive.TransactionalOperator; import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.transaction.support.TransactionTemplate; @@ -177,6 +182,16 @@ class TransactionAutoConfigurationTests { }); } + @Test + void excludesAbstractTransactionAspectFromLazyInit() { + this.contextRunner.withUserConfiguration(AspectJTransactionManagementConfiguration.class) + .withInitializer(new ConditionEvaluationReportLoggingListener(LogLevel.INFO)) + .run((context) -> { + LazyInitializationExcludeFilter filter = context.getBean(LazyInitializationExcludeFilter.class); + assertThat(filter.isExcluded(null, null, AbstractTransactionAspect.class)).isTrue(); + }); + } + @Configuration static class SinglePlatformTransactionManagerConfiguration { @@ -293,6 +308,12 @@ class TransactionAutoConfigurationTests { } + @Configuration(proxyBeanMethods = false) + @EnableTransactionManagement(mode = AdviceMode.ASPECTJ) + static class AspectJTransactionManagementConfiguration { + + } + interface TransactionalService { @Transactional