首页 > 解决方案 > KafkaListener 没有使用“#{__listener.getContainerFactory()}”解析 containerFactory

问题描述

我正在使用以下方法来获取 KafkaListener 属性。但它不解析 containerFactory 值。但它适用于主题和 groupId。

@KafkaListener(topics = "#{__listener.getTopics()}",
            groupId = "#{__listener.getGroupId()}",
            containerFactory =  "#{__listener.getContainerFactory()}")
    public void sampleTestTopicListener(@Payload byte[] data,

.....
 @Value("sampleTestListenerContainerFactory")
    private String containerFactory ;

    public String  getContainerFactory(){
        return containerFactory;
    }
....
Spring-kafka-2.2.5.RELEASE 

I get the following error :
A component required a bean named '#{__listener.getContainerFactory()}' that could not be found.

Changing the containerFactory value to "sampleTestListenerContainerFactory" instead of spEL, it works fine.

如何使用当前的 beans 属性设置它?

标签: spring-kafka

解决方案


该注释containerFactory上的选项@KafkaListener不能作为 SpEL 表达式。不过,您可以使用属性占位符解析。像这样:

containerFactory = "${sampleTestListenerContainerFactory}"

但无论如何最好使用一个真实的bean名称:KafkaListenerContainerFactory真的必须是一个bean......

另一个注意事项:Apache Kafka 2.2.5 的 Spring 版本不支持。它在很久以前就达到了生命的尽头。保持最新版本更实用:https ://spring.io/projects/spring-kafka#learn


推荐阅读