1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

spring-kafkaのKafkaListenerでtopicsなどをメソッドやプロパティ経由で指定

1
Posted at

spring-kafkaのKafkaListenertopicsなどは基本的にはプロパティで指定するが動的にメソッドやプロパティ経由で指定したい場合がある。その場合beanRefと組み合わせて実現する。

@Component
public class SampleConsumer {
	public String getMyTopic() {
		return "mytopic3";
	}
	
//	public String myTopic = "mytopic3";
	
	@KafkaListener(topics = "#{sample.myTopic}", beanRef = "sample")
	public void processMessage(

beanRefにはこのbean自身を指す名前をつける(デフォルトは__listener)。これでSpELでbeanを参照可能になるので、メソッドやプロパティが呼べる。上記例は#{sample.myTopic}getMyTopicを呼んでいる。コメントアウトしているmyTopicプロパティも使用可能。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?