Skip to content

Commit 64ce024

Browse files
artembilanspring-builds
authored andcommitted
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. (cherry picked from commit 4a135a1)
1 parent 304111a commit 64ce024

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;
@@ -152,7 +152,7 @@
152152
* @see MethodKafkaListenerEndpoint
153153
*/
154154
public class KafkaListenerAnnotationBeanPostProcessor<K, V>
155-
implements BeanPostProcessor, Ordered, ApplicationContextAware, InitializingBean, SmartInitializingSingleton {
155+
implements BeanPostProcessor, Ordered, ApplicationContextAware, SmartInitializingSingleton {
156156

157157
private static final String UNCHECKED = "unchecked";
158158

@@ -182,6 +182,8 @@ public class KafkaListenerAnnotationBeanPostProcessor<K, V>
182182

183183
private final AtomicInteger counter = new AtomicInteger();
184184

185+
private final AtomicBoolean enhancerIsBuilt = new AtomicBoolean();
186+
185187
private KafkaListenerEndpointRegistry endpointRegistry;
186188

187189
private String defaultContainerFactoryBeanName = DEFAULT_KAFKA_LISTENER_CONTAINER_FACTORY_BEAN_NAME;
@@ -291,11 +293,6 @@ public void setCharset(Charset charset) {
291293
this.charset = charset;
292294
}
293295

294-
@Override
295-
public void afterPropertiesSet() throws Exception {
296-
buildEnhancer();
297-
}
298-
299296
@Override
300297
public void afterSingletonsInstantiated() {
301298
this.registrar.setBeanFactory(this.beanFactory);
@@ -340,7 +337,7 @@ public void afterSingletonsInstantiated() {
340337
}
341338

342339
private void buildEnhancer() {
343-
if (this.applicationContext != null) {
340+
if (this.applicationContext != null && this.enhancerIsBuilt.compareAndSet(false, true)) {
344341
Map<String, AnnotationEnhancer> enhancersMap =
345342
this.applicationContext.getBeansOfType(AnnotationEnhancer.class, false, false);
346343
if (enhancersMap.size() > 0) {
@@ -366,6 +363,7 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
366363

367364
@Override
368365
public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
366+
buildEnhancer();
369367
if (!this.nonAnnotatedClasses.contains(bean.getClass())) {
370368
Class<?> targetClass = AopUtils.getTargetClass(bean);
371369
Collection<KafkaListener> classLevelListeners = findListenerAnnotations(targetClass);

0 commit comments

Comments
 (0)