I'm writing a Kafka consumer. I need to pass the environment variable topic name to @KafkaListener(topics = ...)
. This is what I have tried so far:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;
@Service
public class KafkaConsumer {
@Autowired
private EnvProperties envProperties;
private final String topic = envProperties.getTopic();
@KafkaListener(topics = "#{'${envProperties.getTopic()}'}", groupId = "group_id")
public void consume(String message) {
logger.info("Consuming messages " +envProperties.getTopic());
}
}
I'm getting an error at the line topics = "#{'${envProperties.getTopic()}'}"
, the application fails to start.
How to set this topic name dynamically from the environment variable?