Skip to content

Commit

Permalink
GH-3544: Support SpEL in @KafkaListener containerPostProcessor (#3548)
Browse files Browse the repository at this point in the history
Fixes: #3544

#3544

- Enhance resolveContainerPostProcessor method in KafkaListenerAnnotationBeanPostProcessor
  to evaluate SpEL expressions
- Verify containerPostProcessor property in KafkaListener annotation can be specified as a SpEL expression

(cherry picked from commit 3f45fc0)

# Conflicts:
#	spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessor.java
  • Loading branch information
sobychacko authored and artembilan committed Oct 9, 2024
1 parent c881f2d commit 581d7e5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -43,6 +43,7 @@
* Tests for container customizations.
*
* @author Francois Rosiere
* @author Soby Chacko
* @since 3.1
*/
@SuppressWarnings("unused")
Expand Down Expand Up @@ -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) {
}

Expand Down

0 comments on commit 581d7e5

Please sign in to comment.