当我尝试使用主题中的数据(主题名称基于用户)时,在运行时我尝试使用主题中的消息,但收到以下错误。
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'consumerProperties' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?
这是我的代码
@Service
public class kafkaConsumerService {
private SimpMessagingTemplate template;
KafkaConsumerProperties consumerProperties;
@Autowired
public kafkaConsumerService(KafkaConsumerProperties consumerProperties, SimpMessagingTemplate template) {
this.consumerProperties=consumerProperties;
this.template=template;
}
@KafkaListener(topics = {"#{consumerProperties.getTopic()}"})
// @KafkaListener(topics="Chandan3706")
public void consume(@Payload Message message) {
System.out.println("from kafka topic::" + message);
template.convertAndSend("/chat/getMessage", message);
}
}
我的KafkaConsumerProperties.class
@Component
@ConfigurationProperties(prefix="kafka.consumer")
public class KafkaConsumerProperties {
private String bootStrap;
private String group;
private String topic;
public String getBootStrap() {
return bootStrap;
}
public void setBootStrap(String bootStrap) {
this.bootStrap = bootStrap;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
@Override
public String toString() {
return "KafkaConsumerProperties [bootStrap=" + bootStrap + ", group=" + group + ", topic=" + topic + "]";
}
}
提前致谢
最佳答案
由于您没有为 KafkaConsumerProperties
组件提供任何 bean 名称,因此默认名称为 de-capitalized class name 。就是这样。
您在@KafkaListener
中使用的表达式是常规的bean definition phase expression ,因此根对象是一些 BeanExpressionContext
,但当您尝试通过该属性进行访问时,它不是您的监听器 bean。
不确定此监听器中是否需要 KafkaConsumerProperties
属性,但表达式必须请求 kafkaConsumerProperties
bean:
@Service
public class kafkaConsumerService {
private SimpMessagingTemplate template;
@Autowired
public kafkaConsumerService(SimpMessagingTemplate template) {
this.template=template;
}
@KafkaListener(topics = {"#{kafkaConsumerProperties.topic}"})
// @KafkaListener(topics="Chandan3706")
public void consume(@Payload Message message) {
System.out.println("from kafka topic::" + message);
template.convertAndSend("/chat/getMessage", message);
}
}
关于spring - 也许不公开或无效?使用 Spring 的 Websocket 和 Kafka,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52252027/