问题描述
我已经通过 application.yaml 和 spring configurationProperties 配置了我的 rabbit 属性.因此,当我配置交换、队列和绑定时,我可以使用我的属性的 getter
I have configured my rabbit properties via application.yaml and spring configurationProperties.Thus, when I configure exchanges, queues and bindings, I can use the getters of my properties
@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 以从队列中记录消息时,我必须使用完整的属性名称,例如
However, when I configure a @RabbitListener to log the messages on from the queue, I have to use the full properties name like
@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,如:
I want to avoid this error prone hard coded String and refer to the configurationProperties bean like:
@RabbitListener(queues = "${properties.getQueue()}")
我有一个 类似问题 使用 @EventListener 使用 bean 引用"@bean.method()" 有帮助,但在这里不起作用,bean 表达式只是被解释为队列名称,由于队列名称@bean...."不存在而失败.
I had a similar issue once with @EventListener where using a bean reference "@bean.method()" helped, but it does not work here, the bean expression is just interpreted as queue name, which fails because a queue namde "@bean...." does not exist.
是否可以使用 ConfigurationProperty-Beans 进行 RabbitListener 队列配置?
Is it possible to use ConfigurationProperty-Beans for RabbitListener queue configuration?
推荐答案
在我只使用 Bean 和 SpEL 的情况下,类似的方法对我有用.
Something like this worked for me where I just used the Bean and SpEL.
@Autowired
Queue queue;
@RabbitListener(queues = "#{queue.getName()}")
这篇关于通过 ConfigurationProperties 的 RabbitListener 注释队列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!