From b25f98374ba924d3b8b61005e64eaf22edd3d529 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 27 Nov 2025 16:00:43 +0100 Subject: [PATCH] Tighten cacheable decision behind @Lazy injection point Closes gh-35917 (cherry picked from commit 61d5413c23cc820b6f2f4408c29408b8b1736ea8) --- ...xtAnnotationAutowireCandidateResolver.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java b/spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java index 418a17d89cd..aafe340a726 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java @@ -172,22 +172,25 @@ public class ContextAnnotationAutowireCandidateResolver extends QualifierAnnotat } } - boolean cacheable = true; - for (String autowiredBeanName : autowiredBeanNames) { - if (!this.beanFactory.containsBean(autowiredBeanName)) { - cacheable = false; - } - else { - if (this.beanName != null) { - this.beanFactory.registerDependentBean(autowiredBeanName, this.beanName); - } - if (!this.beanFactory.isSingleton(autowiredBeanName)) { + boolean cacheable = false; + if (!autowiredBeanNames.isEmpty()) { + cacheable = true; + for (String autowiredBeanName : autowiredBeanNames) { + if (!this.beanFactory.containsBean(autowiredBeanName)) { cacheable = false; } + else { + if (this.beanName != null) { + this.beanFactory.registerDependentBean(autowiredBeanName, this.beanName); + } + if (!this.beanFactory.isSingleton(autowiredBeanName)) { + cacheable = false; + } + } } - if (cacheable) { - this.cachedTarget = target; - } + } + if (cacheable) { + this.cachedTarget = target; } return target;