diff --git a/spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessor.java b/spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessor.java index 1ee5f3f5d9..ce370f929b 100644 --- a/spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessor.java +++ b/spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessor.java @@ -735,11 +735,16 @@ private KafkaListenerContainerFactory resolveContainerFactory(KafkaListener k private void resolveContainerPostProcessor(MethodKafkaListenerEndpoint endpoint, KafkaListener kafkaListener) { - - final String containerPostProcessor = kafkaListener.containerPostProcessor(); - if (StringUtils.hasText(containerPostProcessor)) { - endpoint.setContainerPostProcessor(this.beanFactory.getBean(containerPostProcessor, - ContainerPostProcessor.class)); + Object containerPostProcessor = resolveExpression(kafkaListener.containerPostProcessor()); + if (containerPostProcessor instanceof ContainerPostProcessor cpp) { + endpoint.setContainerPostProcessor(cpp); + } + else { + String containerPostProcessorBeanName = resolveExpressionAsString(kafkaListener.containerPostProcessor(), "containerPostProcessor"); + if (StringUtils.hasText(containerPostProcessorBeanName)) { + endpoint.setContainerPostProcessor( + this.beanFactory.getBean(containerPostProcessorBeanName, ContainerPostProcessor.class)); + } } } diff --git a/spring-kafka/src/test/java/org/springframework/kafka/listener/ContainerCustomizationTests.java b/spring-kafka/src/test/java/org/springframework/kafka/listener/ContainerCustomizationTests.java index b93ff04406..ce8f5f368d 100644 --- a/spring-kafka/src/test/java/org/springframework/kafka/listener/ContainerCustomizationTests.java +++ b/spring-kafka/src/test/java/org/springframework/kafka/listener/ContainerCustomizationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2023 the original author or authors. + * Copyright 2019-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,6 +43,7 @@ * Tests for container customizations. * * @author Francois Rosiere + * @author Soby Chacko * @since 3.1 */ @SuppressWarnings("unused") @@ -129,7 +130,7 @@ public void postProcessor(String foo) { id = CONTAINER_CUSTOMIZER_AND_POST_PROCESSOR, topics = TOPIC, containerFactory = "containerFactoryWithCustomizer", - containerPostProcessor = "infoContainerPostProcessor") + containerPostProcessor = "#{__listener.infoContainerPostProcessor}") public void containerCustomizerAndPostProcessor(String foo) { }