1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
import java .util .Collection ;
21
21
import java .util .Comparator ;
22
22
import java .util .HashSet ;
23
+ import java .util .LinkedHashSet ;
23
24
import java .util .List ;
24
25
import java .util .Set ;
25
26
import java .util .function .BiConsumer ;
@@ -227,7 +228,8 @@ public static void registerBeanPostProcessors(
227
228
// a bean is created during BeanPostProcessor instantiation, i.e. when
228
229
// a bean is not eligible for getting processed by all BeanPostProcessors.
229
230
int beanProcessorTargetCount = beanFactory .getBeanPostProcessorCount () + 1 + postProcessorNames .length ;
230
- beanFactory .addBeanPostProcessor (new BeanPostProcessorChecker (beanFactory , beanProcessorTargetCount ));
231
+ beanFactory .addBeanPostProcessor (
232
+ new BeanPostProcessorChecker (beanFactory , postProcessorNames , beanProcessorTargetCount ));
231
233
232
234
// Separate between BeanPostProcessors that implement PriorityOrdered,
233
235
// Ordered, and the rest.
@@ -389,10 +391,15 @@ private static final class BeanPostProcessorChecker implements BeanPostProcessor
389
391
390
392
private final ConfigurableListableBeanFactory beanFactory ;
391
393
394
+ private final String [] postProcessorNames ;
395
+
392
396
private final int beanPostProcessorTargetCount ;
393
397
394
- public BeanPostProcessorChecker (ConfigurableListableBeanFactory beanFactory , int beanPostProcessorTargetCount ) {
398
+ public BeanPostProcessorChecker (ConfigurableListableBeanFactory beanFactory ,
399
+ String [] postProcessorNames , int beanPostProcessorTargetCount ) {
400
+
395
401
this .beanFactory = beanFactory ;
402
+ this .postProcessorNames = postProcessorNames ;
396
403
this .beanPostProcessorTargetCount = beanPostProcessorTargetCount ;
397
404
}
398
405
@@ -405,10 +412,30 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) {
405
412
public Object postProcessAfterInitialization (Object bean , String beanName ) {
406
413
if (!(bean instanceof BeanPostProcessor ) && !isInfrastructureBean (beanName ) &&
407
414
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 () +
410
435
"] 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." );
412
439
}
413
440
}
414
441
return bean ;
0 commit comments