Search code examples
lambdaspring-annotationsspring-elspring-kafka

Need help in using SPEL in Annotation Value?


Tried below SPEL expression, but it fails to work. Need help!

@KafkaListener(topics = "#{Arrays.asList(${kafka.topic.helloworld}.split(',')).stream().map(p -> p+envSuffix).toArray(String[]::new)}")

Solution

  • Solution is: One of the way to add lambda into annotation is as follows:

    In the KafkaReceiver class's method:

    @Autowired TopicUtil topicUtil;
      
      
    @KafkaListener(topics = "#{topicUtil.suffixTopics()}")
    
    
    //In the TopicUtil - add the follwoing method
    public String[] suffixTopics() {
        return Arrays.asList(pTopics.split(",")).stream().map(p -> p + envSuffix).toArray(String[]::new);
    }