Skip to content

Commit c596ff5

Browse files
committed
Log warn message with specific guidance in BeanPostProcessorChecker
Closes gh-24092
1 parent cc90a95 commit c596ff5

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.util.Collection;
2121
import java.util.Comparator;
2222
import java.util.HashSet;
23+
import java.util.LinkedHashSet;
2324
import java.util.List;
2425
import java.util.Set;
2526
import java.util.function.BiConsumer;
@@ -227,7 +228,8 @@ public static void registerBeanPostProcessors(
227228
// a bean is created during BeanPostProcessor instantiation, i.e. when
228229
// a bean is not eligible for getting processed by all BeanPostProcessors.
229230
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
230-
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
231+
beanFactory.addBeanPostProcessor(
232+
new BeanPostProcessorChecker(beanFactory, postProcessorNames, beanProcessorTargetCount));
231233

232234
// Separate between BeanPostProcessors that implement PriorityOrdered,
233235
// Ordered, and the rest.
@@ -389,10 +391,15 @@ private static final class BeanPostProcessorChecker implements BeanPostProcessor
389391

390392
private final ConfigurableListableBeanFactory beanFactory;
391393

394+
private final String[] postProcessorNames;
395+
392396
private final int beanPostProcessorTargetCount;
393397

394-
public BeanPostProcessorChecker(ConfigurableListableBeanFactory beanFactory, int beanPostProcessorTargetCount) {
398+
public BeanPostProcessorChecker(ConfigurableListableBeanFactory beanFactory,
399+
String[] postProcessorNames, int beanPostProcessorTargetCount) {
400+
395401
this.beanFactory = beanFactory;
402+
this.postProcessorNames = postProcessorNames;
396403
this.beanPostProcessorTargetCount = beanPostProcessorTargetCount;
397404
}
398405

@@ -405,10 +412,30 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) {
405412
public Object postProcessAfterInitialization(Object bean, String beanName) {
406413
if (!(bean instanceof BeanPostProcessor) && !isInfrastructureBean(beanName) &&
407414
this.beanFactory.getBeanPostProcessorCount() < this.beanPostProcessorTargetCount) {
408-
if (logger.isInfoEnabled()) {
409-
logger.info("Bean '" + beanName + "' of type [" + bean.getClass().getName() +
415+
if (logger.isWarnEnabled()) {
416+
Set<String> bppsInCreation = new LinkedHashSet<>(2);
417+
for (String bppName : this.postProcessorNames) {
418+
if (this.beanFactory.isCurrentlyInCreation(bppName)) {
419+
bppsInCreation.add(bppName);
420+
}
421+
}
422+
if (bppsInCreation.size() == 1) {
423+
String bppName = bppsInCreation.iterator().next();
424+
if (this.beanFactory.containsBeanDefinition(bppName) &&
425+
beanName.equals(this.beanFactory.getBeanDefinition(bppName).getFactoryBeanName())) {
426+
logger.warn("Bean '" + beanName + "' of type [" + bean.getClass().getName() +
427+
"] is not eligible for getting processed by all BeanPostProcessors " +
428+
"(for example: not eligible for auto-proxying). The currently created " +
429+
"BeanPostProcessor " + bppsInCreation + " is declared through a non-static " +
430+
"factory method on that class; consider declaring it as static instead.");
431+
return bean;
432+
}
433+
}
434+
logger.warn("Bean '" + beanName + "' of type [" + bean.getClass().getName() +
410435
"] is not eligible for getting processed by all BeanPostProcessors " +
411-
"(for example: not eligible for auto-proxying)");
436+
"(for example: not eligible for auto-proxying). Is this bean getting eagerly " +
437+
"injected into a currently created BeanPostProcessor " + bppsInCreation + "? " +
438+
"Check the corresponding BeanPostProcessor declaration and its dependencies.");
412439
}
413440
}
414441
return bean;

0 commit comments

Comments
 (0)