From 696f48f33c10c04b927dd750622e376321f940cc Mon Sep 17 00:00:00 2001 From: chickenchickenlove Date: Sat, 26 Apr 2025 23:21:01 +0900 Subject: [PATCH 1/5] spring-projectsGH-3869: Should fail bean registration when no method listeners are registered. Signed-off-by: chickenchickenlove --- ...kaListenerAnnotationBeanPostProcessor.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) 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 1edcc0104a..1013709f9d 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 @@ -407,18 +407,32 @@ public Object postProcessAfterInitialization(final Object bean, final String bea this.logger.debug(() -> annotatedMethods.size() + " @KafkaListener methods processed on bean '" + beanName + "': " + annotatedMethods); } + Set methodsWithHandler = MethodIntrospector.selectMethods(targetClass, + (ReflectionUtils.MethodFilter) method -> + AnnotationUtils.findAnnotation(method, KafkaHandler.class) != null); + boolean hasMethodLevelKafkaHandlerAnnotation = !methodsWithHandler.isEmpty(); if (hasClassLevelListeners) { - Set methodsWithHandler = MethodIntrospector.selectMethods(targetClass, - (ReflectionUtils.MethodFilter) method -> - AnnotationUtils.findAnnotation(method, KafkaHandler.class) != null); List multiMethods = new ArrayList<>(methodsWithHandler); processMultiMethodListeners(classLevelListeners, multiMethods, targetClass, bean, beanName); } + throwErrorIfNoListenerMethods(bean, hasMethodLevelListeners, + hasClassLevelListeners, hasMethodLevelKafkaHandlerAnnotation); } } return bean; } + private void throwErrorIfNoListenerMethods(Object bean, boolean hasMethodLevelListeners, + boolean hasClassLevelListeners, boolean hasMethodLevelKafkaHandlerAnnotation) { + if (hasMethodLevelListeners) { + return; + } + + if (hasClassLevelListeners && !hasMethodLevelKafkaHandlerAnnotation) { + throw new IllegalStateException("No kafka listener methods found on bean type: " + bean.getClass()); + } + } + /* * AnnotationUtils.getRepeatableAnnotations does not look at interfaces */ From 6937686e9cf140a468c558055946edff2527f1de Mon Sep 17 00:00:00 2001 From: chickenchickenlove Date: Sun, 27 Apr 2025 10:12:01 +0900 Subject: [PATCH 2/5] Addressing PR review Signed-off-by: chickenchickenlove --- ...stenerAnnotationBeanPostProcessorTest.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessorTest.java diff --git a/spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessorTest.java b/spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessorTest.java new file mode 100644 index 0000000000..ef20cf07af --- /dev/null +++ b/spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessorTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2017-2025 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.kafka.annotation; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** + * @author Sanghyeok An + * + * @since 4.0.0 + */ + +class KafkaListenerAnnotationBeanPostProcessorTest { + + @Test + void ctx_should_be_fail_to_register_bean_when_no_listener_methods_exist() { + // GIVEN + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + ctx.register(TestConfig.class); + + // GIVEN - expected + Class expectedErrorType = BeanCreationException.class; + String expectedErrorMsg = + "Error creating bean with name 'org.springframework.kafka.annotation." + + "KafkaListenerAnnotationBeanPostProcessorTest$TestConfig$BuggyListener': " + + "No kafka listener methods found on bean type: class org.springframework.kafka" + + ".annotation.KafkaListenerAnnotationBeanPostProcessorTest$TestConfig$BuggyListener"; + + // WHEN + THEN + assertThatThrownBy(ctx::refresh) + .isInstanceOf(expectedErrorType) + .hasMessage(expectedErrorMsg); + } + + @Configuration + static class TestConfig { + + @Bean + public KafkaListenerAnnotationBeanPostProcessor kafkaListenerAnnotationBeanPostProcessor() { + return new KafkaListenerAnnotationBeanPostProcessor<>(); + } + + @Component + @KafkaListener + static class BuggyListener { + + public void listen(String message) { + return; + } + } + + } + +} From cc8a42fd00182ee0fb42a683d872ee17b80420a8 Mon Sep 17 00:00:00 2001 From: chickenchickenlove Date: Tue, 29 Apr 2025 08:15:16 +0900 Subject: [PATCH 3/5] Addressing PR review Signed-off-by: chickenchickenlove --- ...kaListenerAnnotationBeanPostProcessor.java | 18 +++------- ...enerAnnotationBeanPostProcessorTests.java} | 33 ++++++------------- 2 files changed, 15 insertions(+), 36 deletions(-) rename spring-kafka/src/test/java/org/springframework/kafka/annotation/{KafkaListenerAnnotationBeanPostProcessorTest.java => KafkaListenerAnnotationBeanPostProcessorTests.java} (55%) 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 1013709f9d..22def892d2 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 @@ -415,24 +415,16 @@ public Object postProcessAfterInitialization(final Object bean, final String bea List multiMethods = new ArrayList<>(methodsWithHandler); processMultiMethodListeners(classLevelListeners, multiMethods, targetClass, bean, beanName); } - throwErrorIfNoListenerMethods(bean, hasMethodLevelListeners, - hasClassLevelListeners, hasMethodLevelKafkaHandlerAnnotation); + + if (!hasMethodLevelListeners && hasClassLevelListeners && + !hasMethodLevelKafkaHandlerAnnotation) { + throw new IllegalStateException("No kafka listener methods found on bean type."); + } } } return bean; } - private void throwErrorIfNoListenerMethods(Object bean, boolean hasMethodLevelListeners, - boolean hasClassLevelListeners, boolean hasMethodLevelKafkaHandlerAnnotation) { - if (hasMethodLevelListeners) { - return; - } - - if (hasClassLevelListeners && !hasMethodLevelKafkaHandlerAnnotation) { - throw new IllegalStateException("No kafka listener methods found on bean type: " + bean.getClass()); - } - } - /* * AnnotationUtils.getRepeatableAnnotations does not look at interfaces */ diff --git a/spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessorTest.java b/spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessorTests.java similarity index 55% rename from spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessorTest.java rename to spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessorTests.java index ef20cf07af..706ee55d0a 100644 --- a/spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessorTest.java +++ b/spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2025 the original author or authors. + * Copyright 2025 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. @@ -20,11 +20,10 @@ import org.springframework.beans.factory.BeanCreationException; import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Sanghyeok An @@ -32,42 +31,30 @@ * @since 4.0.0 */ -class KafkaListenerAnnotationBeanPostProcessorTest { +class KafkaListenerAnnotationBeanPostProcessorTests { @Test void ctx_should_be_fail_to_register_bean_when_no_listener_methods_exist() { - // GIVEN + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(TestConfig.class); - // GIVEN - expected - Class expectedErrorType = BeanCreationException.class; - String expectedErrorMsg = - "Error creating bean with name 'org.springframework.kafka.annotation." - + "KafkaListenerAnnotationBeanPostProcessorTest$TestConfig$BuggyListener': " - + "No kafka listener methods found on bean type: class org.springframework.kafka" - + ".annotation.KafkaListenerAnnotationBeanPostProcessorTest$TestConfig$BuggyListener"; + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy(ctx::refresh) + .withMessageContaining("No kafka listener methods found on bean type.") + .withMessageContaining("NoHandlerMethodListener"); - // WHEN + THEN - assertThatThrownBy(ctx::refresh) - .isInstanceOf(expectedErrorType) - .hasMessage(expectedErrorMsg); } + @EnableKafka @Configuration static class TestConfig { - @Bean - public KafkaListenerAnnotationBeanPostProcessor kafkaListenerAnnotationBeanPostProcessor() { - return new KafkaListenerAnnotationBeanPostProcessor<>(); - } - @Component @KafkaListener - static class BuggyListener { + static class NoHandlerMethodListener { public void listen(String message) { - return; } } From c3f44f1538a0fc3a3a839e3907a63deca25b27b0 Mon Sep 17 00:00:00 2001 From: ChickenchickenLove Date: Wed, 30 Apr 2025 00:17:49 +0900 Subject: [PATCH 4/5] Update spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessor.java Co-authored-by: Artem Bilan Signed-off-by: ChickenchickenLove --- .../annotation/KafkaListenerAnnotationBeanPostProcessor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 22def892d2..99faf75b7d 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 @@ -418,7 +418,7 @@ public Object postProcessAfterInitialization(final Object bean, final String bea if (!hasMethodLevelListeners && hasClassLevelListeners && !hasMethodLevelKafkaHandlerAnnotation) { - throw new IllegalStateException("No kafka listener methods found on bean type."); + throw new IllegalStateException("No Kafka listener methods in bean: " + bean); } } } From 827e308ea5bb563730f738101faa9518ee853cfc Mon Sep 17 00:00:00 2001 From: chickenchickenlove Date: Wed, 30 Apr 2025 00:27:38 +0900 Subject: [PATCH 5/5] Addressing PR review Signed-off-by: chickenchickenlove --- ...afkaListenerAnnotationBeanPostProcessor.java | 17 ++++++++--------- ...istenerAnnotationBeanPostProcessorTests.java | 3 ++- 2 files changed, 10 insertions(+), 10 deletions(-) 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 99faf75b7d..cf26bed6ae 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 @@ -407,19 +407,18 @@ public Object postProcessAfterInitialization(final Object bean, final String bea this.logger.debug(() -> annotatedMethods.size() + " @KafkaListener methods processed on bean '" + beanName + "': " + annotatedMethods); } - Set methodsWithHandler = MethodIntrospector.selectMethods(targetClass, - (ReflectionUtils.MethodFilter) method -> - AnnotationUtils.findAnnotation(method, KafkaHandler.class) != null); - boolean hasMethodLevelKafkaHandlerAnnotation = !methodsWithHandler.isEmpty(); if (hasClassLevelListeners) { + Set methodsWithHandler = MethodIntrospector.selectMethods(targetClass, + (ReflectionUtils.MethodFilter) method -> + AnnotationUtils.findAnnotation(method, KafkaHandler.class) != null); + + if (methodsWithHandler.isEmpty()) { + throw new IllegalStateException("No Kafka listener methods in bean: " + bean); + } + List multiMethods = new ArrayList<>(methodsWithHandler); processMultiMethodListeners(classLevelListeners, multiMethods, targetClass, bean, beanName); } - - if (!hasMethodLevelListeners && hasClassLevelListeners && - !hasMethodLevelKafkaHandlerAnnotation) { - throw new IllegalStateException("No Kafka listener methods in bean: " + bean); - } } } return bean; diff --git a/spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessorTests.java b/spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessorTests.java index 706ee55d0a..af2a9e08ed 100644 --- a/spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessorTests.java +++ b/spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessorTests.java @@ -41,7 +41,8 @@ void ctx_should_be_fail_to_register_bean_when_no_listener_methods_exist() { assertThatExceptionOfType(BeanCreationException.class) .isThrownBy(ctx::refresh) - .withMessageContaining("No kafka listener methods found on bean type.") + .withRootCauseInstanceOf(IllegalStateException.class) + .withMessageContaining("No Kafka listener methods in bean:") .withMessageContaining("NoHandlerMethodListener"); }