【问题标题】:Read from configuration kafkalistener从配置 kafkalistener 中读取
【发布时间】:2017-11-05 02:15:28
【问题描述】:

我有一个 Spring Boot 应用程序,它的监听器如下:

@KafkaListener(id = "demo", topics = "demo",
            containerFactory = "retryKafkaListenerContainerFactory")
    public void receive(ConsumerRecord<String, String> consumerRecord, Acknowledgment acknowledgment) throws Exception {

}

我有 apache 配置对象,我想用它来从属性中读取主题。我知道我可以使用属性占位符来做到这一点。但是我使用的配置内部有一些逻辑,所以只想从该配置对象中读取。如下:

@Inject
private Configuration configuration

我可以通过configuration.getString("kafka.consumer.topic") 获取主题。我尝试像这样使用:topics = "#{configuration.getString('kafka-generic.consumer.topics')}" 在 KafkaListener 注释的主题字段中,但出现以下错误。

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'configuration' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:224)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:81)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:51)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87)
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120)
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:242)
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:161)
    ... 23 common frames omitted

谁能告诉我如何在KafkaListener注解的topics字段中使用configuration.getString("kafka.consumer.topic")

【问题讨论】:

    标签: java spring apache-kafka kafka-consumer-api spring-kafka


    【解决方案1】:

    出现错误。

    对于这样的问题,这永远不够;您必须显示实际错误。

    您可以使用 SpEL topics = "#{@somebean.someProperty}" ortopics = "#{@somebean.getString('...')}"

    【讨论】:

    • 嘿 Gary,这很有帮助,但在更改为 topics = "#{configuration.getString('kafka-generic.consumer.topics')}" 后,我遇到了以下错误。 ` 原因:org.springframework.expression.spel.SpelEvaluationException:EL1008E:在“org.springframework.beans.factory.config.BeanExpressionContext”类型的对象上找不到属性或字段“配置”-可能不公开?在 org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:224)。你能帮忙吗`
    • 该错误表明应用程序上下文中没有名称为 configuration 的 bean。 @ 是可选的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 2018-11-29
    • 2021-12-24
    相关资源
    最近更新 更多