我已经通过 application.yaml 和 spring configurationProperties 配置了我的 rabbit 属性。 因此,当我配置交换、队列和绑定(bind)时,我可以使用我的属性的 getter
@Bean Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(properties.getQueue());
}
@Bean Queue queue() {
return new Queue(properties.getQueue(), true);
}
@Bean TopicExchange exchange() {
return new TopicExchange(properties.getExchange());
}
但是,当我配置 @RabbitListener 以从队列中记录消息时,我必须使用完整的属性名称,例如
@RabbitListener(queues = "${some.long.path.to.the.queue.name}")
public void onMessage(
final Message message, final Channel channel) throws Exception {
log.info("receiving message: {}#{}", message, channel);
}
我想避免这种容易出错的硬编码字符串,并引用 configurationProperties bean,如:
@RabbitListener(queues = "${properties.getQueue()}")
我有一个 similar issue曾经与@EventListener 一起使用bean 引用“@bean.method()”有帮助,但它在这里不起作用,bean 表达式只是被解释为队列名称,因为队列名称“@bean ....”而失败不存在。
是否可以使用 ConfigurationProperty-Beans 进行 RabbitListener 队列配置?
最佳答案
在我刚刚使用 Bean 和 SpEL 的情况下,类似的方法对我有用。
@Autowired
Queue queue;
@RabbitListener(queues = "#{queue.getName()}")
关于spring-boot - 通过 ConfigurationProperties 的 RabbitListener 注释队列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49055634/