|
20 | 20 | import java.io.StringReader; |
21 | 21 | import java.lang.reflect.AnnotatedElement; |
22 | 22 | import java.lang.reflect.Method; |
| 23 | +import java.lang.reflect.Modifier; |
23 | 24 | import java.nio.ByteBuffer; |
24 | 25 | import java.nio.charset.Charset; |
25 | 26 | import java.nio.charset.StandardCharsets; |
|
33 | 34 | import java.util.Locale; |
34 | 35 | import java.util.Map; |
35 | 36 | import java.util.Objects; |
36 | | -import java.util.Optional; |
37 | 37 | import java.util.Properties; |
38 | 38 | import java.util.Set; |
39 | 39 | import java.util.concurrent.ConcurrentHashMap; |
|
149 | 149 | * @author Sanghyeok An |
150 | 150 | * @author Soby Chacko |
151 | 151 | * @author Omer Celik |
152 | | - * @author Sanghyeok An |
153 | 152 | * |
154 | 153 | * @see KafkaListener |
155 | 154 | * @see KafkaListenerErrorHandler |
|
159 | 158 | * @see KafkaListenerEndpointRegistry |
160 | 159 | * @see org.springframework.kafka.config.KafkaListenerEndpoint |
161 | 160 | * @see MethodKafkaListenerEndpoint |
162 | | - * @see KafkaHandlerParser |
163 | 161 | */ |
164 | 162 | public class KafkaListenerAnnotationBeanPostProcessor<K, V> |
165 | 163 | implements BeanPostProcessor, Ordered, ApplicationContextAware, SmartInitializingSingleton { |
@@ -216,8 +214,6 @@ public class KafkaListenerAnnotationBeanPostProcessor<K, V> |
216 | 214 |
|
217 | 215 | private final Lock globalLock = new ReentrantLock(); |
218 | 216 |
|
219 | | - private final KafkaHandlerParser kafkaHandlerParser = new KafkaHandlerParser(); |
220 | | - |
221 | 217 | @Override |
222 | 218 | public int getOrder() { |
223 | 219 | return LOWEST_PRECEDENCE; |
@@ -413,16 +409,23 @@ public Object postProcessAfterInitialization(final Object bean, final String bea |
413 | 409 | + beanName + "': " + annotatedMethods); |
414 | 410 | } |
415 | 411 | if (hasClassLevelListeners) { |
416 | | - Optional<Method> methodWithoutAnnotation = this.kafkaHandlerParser.parseSingleHandlerMethod(targetClass); |
417 | | - if (methodWithoutAnnotation.isPresent() && classLevelListeners.size() == 1) { |
| 412 | + Set<Method> methodsWithHandler = MethodIntrospector.selectMethods(targetClass, |
| 413 | + (ReflectionUtils.MethodFilter) method -> |
| 414 | + AnnotationUtils.findAnnotation(method, KafkaHandler.class) != null); |
| 415 | + Set<Method> publicMethods = MethodIntrospector.selectMethods(targetClass, |
| 416 | + (ReflectionUtils.MethodFilter) method -> |
| 417 | + Modifier.isPublic(method.getModifiers())); |
| 418 | + |
| 419 | + if (methodsWithHandler.isEmpty() && publicMethods.size() == 1 && !hasMethodLevelListeners) { |
418 | 420 | // Case when target class has class-level @KafkaListener annotation and |
419 | 421 | // has only single public method without @KafkaHandler. |
420 | | - for (KafkaListener kafkaListener : classLevelListeners) { |
421 | | - processKafkaListener(kafkaListener, methodWithoutAnnotation.get(), bean, beanName); |
| 422 | + // See, GH-3807 for more details. |
| 423 | + Method method = publicMethods.iterator().next(); |
| 424 | + for (KafkaListener classLevelListener : classLevelListeners) { |
| 425 | + processKafkaListener(classLevelListener, method, bean, beanName); |
422 | 426 | } |
423 | 427 | } |
424 | 428 | else { |
425 | | - Set<Method> methodsWithHandler = MethodIntrospector.selectMethods(targetClass, (ReflectionUtils.MethodFilter) method -> AnnotationUtils.findAnnotation(method, KafkaHandler.class) != null); |
426 | 429 | List<Method> multiMethods = new ArrayList<>(methodsWithHandler); |
427 | 430 | processMultiMethodListeners(classLevelListeners, multiMethods, targetClass, bean, beanName); |
428 | 431 | } |
|
0 commit comments