Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-3467: Fix KafkaListenerAnnotationBeanPostProcessor for early BF access #3468

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction;
import java.util.regex.Pattern;
Expand All @@ -46,7 +47,6 @@
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.ObjectFactory;
Expand Down Expand Up @@ -154,7 +154,7 @@
* @see MethodKafkaListenerEndpoint
*/
public class KafkaListenerAnnotationBeanPostProcessor<K, V>
implements BeanPostProcessor, Ordered, ApplicationContextAware, InitializingBean, SmartInitializingSingleton {
implements BeanPostProcessor, Ordered, ApplicationContextAware, SmartInitializingSingleton {

private static final String UNCHECKED = "unchecked";

Expand Down Expand Up @@ -184,6 +184,8 @@ public class KafkaListenerAnnotationBeanPostProcessor<K, V>

private final AtomicInteger counter = new AtomicInteger();

private final AtomicBoolean enhancerIsBuilt = new AtomicBoolean();

private KafkaListenerEndpointRegistry endpointRegistry;

private String defaultContainerFactoryBeanName = DEFAULT_KAFKA_LISTENER_CONTAINER_FACTORY_BEAN_NAME;
Expand Down Expand Up @@ -297,11 +299,6 @@ public void setCharset(Charset charset) {
this.charset = charset;
}

@Override
public void afterPropertiesSet() throws Exception {
buildEnhancer();
}

@Override
public void afterSingletonsInstantiated() {
this.registrar.setBeanFactory(this.beanFactory);
Expand Down Expand Up @@ -348,7 +345,7 @@ public void afterSingletonsInstantiated() {
}

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

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