Skip to content

Commit 696f48f

Browse files
GH-3869: Should fail bean registration when no method listeners are registered.
Signed-off-by: chickenchickenlove <[email protected]>
1 parent a52facc commit 696f48f

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,18 +407,32 @@ public Object postProcessAfterInitialization(final Object bean, final String bea
407407
this.logger.debug(() -> annotatedMethods.size() + " @KafkaListener methods processed on bean '"
408408
+ beanName + "': " + annotatedMethods);
409409
}
410+
Set<Method> methodsWithHandler = MethodIntrospector.selectMethods(targetClass,
411+
(ReflectionUtils.MethodFilter) method ->
412+
AnnotationUtils.findAnnotation(method, KafkaHandler.class) != null);
413+
boolean hasMethodLevelKafkaHandlerAnnotation = !methodsWithHandler.isEmpty();
410414
if (hasClassLevelListeners) {
411-
Set<Method> methodsWithHandler = MethodIntrospector.selectMethods(targetClass,
412-
(ReflectionUtils.MethodFilter) method ->
413-
AnnotationUtils.findAnnotation(method, KafkaHandler.class) != null);
414415
List<Method> multiMethods = new ArrayList<>(methodsWithHandler);
415416
processMultiMethodListeners(classLevelListeners, multiMethods, targetClass, bean, beanName);
416417
}
418+
throwErrorIfNoListenerMethods(bean, hasMethodLevelListeners,
419+
hasClassLevelListeners, hasMethodLevelKafkaHandlerAnnotation);
417420
}
418421
}
419422
return bean;
420423
}
421424

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+
422436
/*
423437
* AnnotationUtils.getRepeatableAnnotations does not look at interfaces
424438
*/

0 commit comments

Comments
 (0)