From b33671239bf0b2b0efcd77a95bd7920f52425878 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Mon, 18 Apr 2022 14:04:52 -0400 Subject: [PATCH] JDK17; Spring Framework 6.0.0-SNAPSHOT Defer fetching RetryListener beans due to `BeanCurrentlyInCreationException` with modern Spring Framework. See gh-286 --- pom.xml | 4 ++-- .../retry/annotation/RetryConfiguration.java | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 878e7c9..2188899 100644 --- a/pom.xml +++ b/pom.xml @@ -28,12 +28,12 @@ 2.0.0-SNAPSHOT false - 1.8 + 17 true UTF-8 UTF-8 - 4.3.29.RELEASE + 6.0.0-SNAPSHOT diff --git a/src/main/java/org/springframework/retry/annotation/RetryConfiguration.java b/src/main/java/org/springframework/retry/annotation/RetryConfiguration.java index dac3cb5..44af456 100644 --- a/src/main/java/org/springframework/retry/annotation/RetryConfiguration.java +++ b/src/main/java/org/springframework/retry/annotation/RetryConfiguration.java @@ -39,6 +39,7 @@ import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.ListableBeanFactory; +import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.Role; import org.springframework.core.OrderComparator; @@ -51,7 +52,6 @@ import org.springframework.retry.policy.RetryContextCache; import org.springframework.stereotype.Component; import org.springframework.util.ObjectUtils; import org.springframework.util.ReflectionUtils; -import org.springframework.util.ReflectionUtils.MethodCallback; /** * Basic configuration for @Retryable processing. For stateful retry, if @@ -62,6 +62,7 @@ import org.springframework.util.ReflectionUtils.MethodCallback; * @author Dave Syer * @author Artem Bilan * @author Markus Heiden + * @author Gary Russell * @since 1.1 * */ @@ -69,9 +70,9 @@ import org.springframework.util.ReflectionUtils.MethodCallback; @Role(BeanDefinition.ROLE_INFRASTRUCTURE) @Component public class RetryConfiguration extends AbstractPointcutAdvisor - implements IntroductionAdvisor, BeanFactoryAware, InitializingBean { + implements IntroductionAdvisor, BeanFactoryAware, InitializingBean, SmartInitializingSingleton { - private Advice advice; + private AnnotationAwareRetryOperationsInterceptor advice; private Pointcut pointcut; @@ -92,7 +93,6 @@ public class RetryConfiguration extends AbstractPointcutAdvisor this.retryContextCache = findBean(RetryContextCache.class); this.methodArgumentsKeyGenerator = findBean(MethodArgumentsKeyGenerator.class); this.newMethodArgumentsIdentifier = findBean(NewMethodArgumentsIdentifier.class); - this.retryListeners = findBeans(RetryListener.class); this.sleeper = findBean(Sleeper.class); Set> retryableAnnotationTypes = new LinkedHashSet<>(1); retryableAnnotationTypes.add(Retryable.class); @@ -103,6 +103,14 @@ public class RetryConfiguration extends AbstractPointcutAdvisor } } + @Override + public void afterSingletonsInstantiated() { + this.retryListeners = findBeans(RetryListener.class); + if (this.retryListeners != null) { + this.advice.setListeners(this.retryListeners); + } + } + private List findBeans(Class type) { if (this.beanFactory instanceof ListableBeanFactory) { ListableBeanFactory listable = (ListableBeanFactory) this.beanFactory; @@ -157,14 +165,11 @@ public class RetryConfiguration extends AbstractPointcutAdvisor return this.pointcut; } - protected Advice buildAdvice() { + protected AnnotationAwareRetryOperationsInterceptor buildAdvice() { AnnotationAwareRetryOperationsInterceptor interceptor = new AnnotationAwareRetryOperationsInterceptor(); if (this.retryContextCache != null) { interceptor.setRetryContextCache(this.retryContextCache); } - if (this.retryListeners != null) { - interceptor.setListeners(this.retryListeners); - } if (this.methodArgumentsKeyGenerator != null) { interceptor.setKeyGenerator(this.methodArgumentsKeyGenerator); }