Skip to content

Commit 4a135a1

Browse files
authored
GH-3467: Fix KafkaListenerAnnotationBeanPostProcessor for early BF access (#3468)
Fixes: #3467 #3467 The `KafkaListenerAnnotationBeanPostProcessor.afterPropertiesSet()` gets access to `BeanFactory` for other beans which causes a warning into logs like: ``` is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying). Is this bean getting eagerly injected into a currently created BeanPostProcessor ``` The rule of thumb is not to have any bean factory access from `BeanPostProcessor` initialization phase. * Fix `KafkaListenerAnnotationBeanPostProcessor` to `buildEnhancer()` in lazy manner. Therefore remove an `afterPropertiesSet()` altogether. **Auto-cherry-pick to `3.2.x` & `3.1.x`**
1 parent 4dc0976 commit 4a135a1

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessor.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.Properties;
3535
import java.util.Set;
3636
import java.util.concurrent.ConcurrentHashMap;
37+
import java.util.concurrent.atomic.AtomicBoolean;
3738
import java.util.concurrent.atomic.AtomicInteger;
3839
import java.util.function.BiFunction;
3940
import java.util.regex.Pattern;
@@ -46,7 +47,6 @@
4647
import org.springframework.beans.BeansException;
4748
import org.springframework.beans.factory.BeanFactory;
4849
import org.springframework.beans.factory.BeanInitializationException;
49-
import org.springframework.beans.factory.InitializingBean;
5050
import org.springframework.beans.factory.ListableBeanFactory;
5151
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
5252
import org.springframework.beans.factory.ObjectFactory;
@@ -154,7 +154,7 @@
154154
* @see MethodKafkaListenerEndpoint
155155
*/
156156
public class KafkaListenerAnnotationBeanPostProcessor<K, V>
157-
implements BeanPostProcessor, Ordered, ApplicationContextAware, InitializingBean, SmartInitializingSingleton {
157+
implements BeanPostProcessor, Ordered, ApplicationContextAware, SmartInitializingSingleton {
158158

159159
private static final String UNCHECKED = "unchecked";
160160

@@ -184,6 +184,8 @@ public class KafkaListenerAnnotationBeanPostProcessor<K, V>
184184

185185
private final AtomicInteger counter = new AtomicInteger();
186186

187+
private final AtomicBoolean enhancerIsBuilt = new AtomicBoolean();
188+
187189
private KafkaListenerEndpointRegistry endpointRegistry;
188190

189191
private String defaultContainerFactoryBeanName = DEFAULT_KAFKA_LISTENER_CONTAINER_FACTORY_BEAN_NAME;
@@ -297,11 +299,6 @@ public void setCharset(Charset charset) {
297299
this.charset = charset;
298300
}
299301

300-
@Override
301-
public void afterPropertiesSet() throws Exception {
302-
buildEnhancer();
303-
}
304-
305302
@Override
306303
public void afterSingletonsInstantiated() {
307304
this.registrar.setBeanFactory(this.beanFactory);
@@ -348,7 +345,7 @@ public void afterSingletonsInstantiated() {
348345
}
349346

350347
private void buildEnhancer() {
351-
if (this.applicationContext != null) {
348+
if (this.applicationContext != null && this.enhancerIsBuilt.compareAndSet(false, true)) {
352349
Map<String, AnnotationEnhancer> enhancersMap =
353350
this.applicationContext.getBeansOfType(AnnotationEnhancer.class, false, false);
354351
if (!enhancersMap.isEmpty()) {
@@ -374,6 +371,7 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
374371

375372
@Override
376373
public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
374+
buildEnhancer();
377375
if (!this.nonAnnotatedClasses.contains(bean.getClass())) {
378376
Class<?> targetClass = AopUtils.getTargetClass(bean);
379377
Collection<KafkaListener> classLevelListeners = findListenerAnnotations(targetClass);

0 commit comments

Comments
 (0)