Skip to content

Commit cc8a42f

Browse files
Addressing PR review
Signed-off-by: chickenchickenlove <[email protected]>
1 parent 6937686 commit cc8a42f

File tree

2 files changed

+15
-36
lines changed

2 files changed

+15
-36
lines changed

spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessor.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -415,24 +415,16 @@ public Object postProcessAfterInitialization(final Object bean, final String bea
415415
List<Method> multiMethods = new ArrayList<>(methodsWithHandler);
416416
processMultiMethodListeners(classLevelListeners, multiMethods, targetClass, bean, beanName);
417417
}
418-
throwErrorIfNoListenerMethods(bean, hasMethodLevelListeners,
419-
hasClassLevelListeners, hasMethodLevelKafkaHandlerAnnotation);
418+
419+
if (!hasMethodLevelListeners && hasClassLevelListeners &&
420+
!hasMethodLevelKafkaHandlerAnnotation) {
421+
throw new IllegalStateException("No kafka listener methods found on bean type.");
422+
}
420423
}
421424
}
422425
return bean;
423426
}
424427

425-
private void throwErrorIfNoListenerMethods(Object bean, boolean hasMethodLevelListeners,
426-
boolean hasClassLevelListeners, boolean hasMethodLevelKafkaHandlerAnnotation) {
427-
if (hasMethodLevelListeners) {
428-
return;
429-
}
430-
431-
if (hasClassLevelListeners && !hasMethodLevelKafkaHandlerAnnotation) {
432-
throw new IllegalStateException("No kafka listener methods found on bean type: " + bean.getClass());
433-
}
434-
}
435-
436428
/*
437429
* AnnotationUtils.getRepeatableAnnotations does not look at interfaces
438430
*/
Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2025 the original author or authors.
2+
* Copyright 2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,54 +20,41 @@
2020

2121
import org.springframework.beans.factory.BeanCreationException;
2222
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
23-
import org.springframework.context.annotation.Bean;
2423
import org.springframework.context.annotation.Configuration;
2524
import org.springframework.stereotype.Component;
2625

27-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
26+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2827

2928
/**
3029
* @author Sanghyeok An
3130
*
3231
* @since 4.0.0
3332
*/
3433

35-
class KafkaListenerAnnotationBeanPostProcessorTest {
34+
class KafkaListenerAnnotationBeanPostProcessorTests {
3635

3736
@Test
3837
void ctx_should_be_fail_to_register_bean_when_no_listener_methods_exist() {
39-
// GIVEN
38+
4039
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
4140
ctx.register(TestConfig.class);
4241

43-
// GIVEN - expected
44-
Class<?> expectedErrorType = BeanCreationException.class;
45-
String expectedErrorMsg =
46-
"Error creating bean with name 'org.springframework.kafka.annotation."
47-
+ "KafkaListenerAnnotationBeanPostProcessorTest$TestConfig$BuggyListener': "
48-
+ "No kafka listener methods found on bean type: class org.springframework.kafka"
49-
+ ".annotation.KafkaListenerAnnotationBeanPostProcessorTest$TestConfig$BuggyListener";
42+
assertThatExceptionOfType(BeanCreationException.class)
43+
.isThrownBy(ctx::refresh)
44+
.withMessageContaining("No kafka listener methods found on bean type.")
45+
.withMessageContaining("NoHandlerMethodListener");
5046

51-
// WHEN + THEN
52-
assertThatThrownBy(ctx::refresh)
53-
.isInstanceOf(expectedErrorType)
54-
.hasMessage(expectedErrorMsg);
5547
}
5648

49+
@EnableKafka
5750
@Configuration
5851
static class TestConfig {
5952

60-
@Bean
61-
public KafkaListenerAnnotationBeanPostProcessor<Object, Object> kafkaListenerAnnotationBeanPostProcessor() {
62-
return new KafkaListenerAnnotationBeanPostProcessor<>();
63-
}
64-
6553
@Component
6654
@KafkaListener
67-
static class BuggyListener {
55+
static class NoHandlerMethodListener {
6856

6957
public void listen(String message) {
70-
return;
7158
}
7259
}
7360

0 commit comments

Comments
 (0)