Skip to content

Commit 3030094

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** # Conflicts: # spring-kafka/src/test/java/org/springframework/kafka/annotation/AliasPropertiesTests.java
1 parent 2938174 commit 3030094

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
@@ -318,7 +318,7 @@ public Object postProcessAfterInitialization(final Object bean, final String bea
318318
*/
319319
private Collection<KafkaListener> findListenerAnnotations(Class<?> clazz) {
320320
Set<KafkaListener> listeners = new HashSet<>();
321-
KafkaListener ann = AnnotationUtils.findAnnotation(clazz, KafkaListener.class);
321+
KafkaListener ann = AnnotatedElementUtils.findMergedAnnotation(clazz, KafkaListener.class);
322322
if (ann != null) {
323323
listeners.add(ann);
324324
}

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.KafkaListenerContainerFactory;
3839
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
@@ -41,6 +42,7 @@
4142
import org.springframework.kafka.core.ProducerFactory;
4243
import org.springframework.kafka.test.EmbeddedKafkaBroker;
4344
import org.springframework.kafka.test.utils.KafkaTestUtils;
45+
import org.springframework.stereotype.Component;
4446
import org.springframework.test.annotation.DirtiesContext;
4547
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
4648

@@ -59,10 +61,14 @@ public class AliasPropertiesTests {
5961
@Autowired
6062
private Config config;
6163

64+
@Autowired
65+
private ClassLevelListener classLevel;
66+
6267
@Test
6368
public void testAliasFor() throws Exception {
6469
this.template.send("alias.tests", "foo");
6570
assertThat(this.config.latch.await(10, TimeUnit.SECONDS)).isTrue();
71+
assertThat(this.classLevel.latch.await(10, TimeUnit.SECONDS)).isTrue();
6672
}
6773

6874
@Configuration
@@ -112,18 +118,34 @@ public Map<String, Object> producerConfigs() {
112118
return KafkaTestUtils.producerProps(embeddedKafka());
113119
}
114120

115-
@MyListener("alias.tests")
121+
@MyListener(id = "onMethod", value = "alias.tests")
116122
public void listen1(String in) {
117-
latch.countDown();
123+
this.latch.countDown();
124+
}
125+
126+
@Component
127+
@MyListener(id = "onClass", value = "alias.tests")
128+
public static class ClassLevelListener {
129+
130+
private final CountDownLatch latch = new CountDownLatch(1);
131+
132+
@KafkaHandler
133+
void listen(String in) {
134+
this.latch.countDown();
135+
}
136+
118137
}
119138

120139
}
121140

122-
@Target(ElementType.METHOD)
141+
@Target({ ElementType.METHOD, ElementType.TYPE })
123142
@Retention(RetentionPolicy.RUNTIME)
124143
@KafkaListener
125144
public @interface MyListener {
126145

146+
@AliasFor(annotation = KafkaListener.class, attribute = "id")
147+
String id() default "";
148+
127149
@AliasFor(annotation = KafkaListener.class, attribute = "topics")
128150
String[] value();
129151

0 commit comments

Comments
 (0)