【发布时间】: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