Skip to content

Commit 4a583b5

Browse files
garyrussellartembilan
authored andcommitted
GH-1487: @KafkaListener Meta-Annotation on Class
Resolves #1487 A meta-annotated annotation did not work at the class level. **cherry-pick to 2.4.x, 2.3.x, 2.2.x**
1 parent 38fdafd commit 4a583b5

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public Object postProcessAfterInitialization(final Object bean, final String bea
313313
*/
314314
private Collection<KafkaListener> findListenerAnnotations(Class<?> clazz) {
315315
Set<KafkaListener> listeners = new HashSet<>();
316-
KafkaListener ann = AnnotationUtils.findAnnotation(clazz, KafkaListener.class);
316+
KafkaListener ann = AnnotatedElementUtils.findMergedAnnotation(clazz, KafkaListener.class);
317317
if (ann != null) {
318318
listeners.add(ann);
319319
}

spring-kafka/src/test/java/org/springframework/kafka/annotation/AliasPropertiesTests.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.context.annotation.Bean;
3434
import org.springframework.context.annotation.Configuration;
3535
import org.springframework.core.annotation.AliasFor;
36+
import org.springframework.kafka.annotation.AliasPropertiesTests.Config.ClassLevelListener;
3637
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
3738
import org.springframework.kafka.config.KafkaListenerConfigUtils;
3839
import org.springframework.kafka.config.KafkaListenerContainerFactory;
@@ -43,6 +44,7 @@
4344
import org.springframework.kafka.core.ProducerFactory;
4445
import org.springframework.kafka.test.EmbeddedKafkaBroker;
4546
import org.springframework.kafka.test.utils.KafkaTestUtils;
47+
import org.springframework.stereotype.Component;
4648
import org.springframework.test.annotation.DirtiesContext;
4749
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
4850

@@ -66,11 +68,15 @@ public class AliasPropertiesTests {
6668
@Autowired
6769
private KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry;
6870

71+
@Autowired
72+
private ClassLevelListener classLevel;
73+
6974
@Test
7075
public void testAliasFor() throws Exception {
7176
this.template.send("alias.tests", "foo");
7277
assertThat(this.config.latch.await(10, TimeUnit.SECONDS)).isTrue();
7378
assertThat(this.config.kafkaListenerEndpointRegistry()).isSameAs(this.kafkaListenerEndpointRegistry);
79+
assertThat(this.classLevel.latch.await(10, TimeUnit.SECONDS)).isTrue();
7480
}
7581

7682
@Configuration
@@ -125,18 +131,34 @@ public Map<String, Object> producerConfigs() {
125131
return KafkaTestUtils.producerProps(embeddedKafka());
126132
}
127133

128-
@MyListener("alias.tests")
134+
@MyListener(id = "onMethod", value = "alias.tests")
129135
public void listen1(String in) {
130-
latch.countDown();
136+
this.latch.countDown();
137+
}
138+
139+
@Component
140+
@MyListener(id = "onClass", value = "alias.tests")
141+
public static class ClassLevelListener {
142+
143+
private final CountDownLatch latch = new CountDownLatch(1);
144+
145+
@KafkaHandler
146+
void listen(String in) {
147+
this.latch.countDown();
148+
}
149+
131150
}
132151

133152
}
134153

135-
@Target(ElementType.METHOD)
154+
@Target({ ElementType.METHOD, ElementType.TYPE })
136155
@Retention(RetentionPolicy.RUNTIME)
137156
@KafkaListener
138157
public @interface MyListener {
139158

159+
@AliasFor(annotation = KafkaListener.class, attribute = "id")
160+
String id() default "";
161+
140162
@AliasFor(annotation = KafkaListener.class, attribute = "topics")
141163
String[] value();
142164

0 commit comments

Comments
 (0)