如何使用基于java的配置将队列绑定到exchange
我正试图通过spring引导和基于java的配置来理解RabbitMQ。我在github中遇到一段代码,其中配置了两个队列。请查看下面的代码片段:如何使用基于java的配置将队列绑定到exchange,java,spring,spring-boot,rabbitmq,Java,Spring,Spring Boot,Rabbitmq,我正试图通过spring引导和基于java的配置来理解RabbitMQ。我在github中遇到一段代码,其中配置了两个队列。请查看下面的代码片段: @Bean Queue queueFoo() { return new Queue("queue.foo", false); } @Bean Queue queueBar() { return new Queue("queue.bar", false); } @Bean TopicExchange exchange() {
@Bean
Queue queueFoo() {
return new Queue("queue.foo", false);
}
@Bean
Queue queueBar() {
return new Queue("queue.bar", false);
}
@Bean
TopicExchange exchange() {
return new TopicExchange("exchange");
}
@Bean
Binding bindingExchangeFoo(Queue queueFoo, TopicExchange exchange) {
return BindingBuilder.bind(queueFoo).to(exchange).with("queue.foo");
}
@Bean
Binding bindingExchangeBar(Queue queueBar, TopicExchange exchange) {
return BindingBuilder.bind(queueBar).to(exchange).with("queue.bar");
}
有两个队列Bean定义-queueFoo和queueBar。绑定配置正确吗??在该行中—
Binding bindingExchangeFoo(Queue queueFoo, TopicExchange exchange) {
那么,参数-queueFoo的名称是否必须与Queue的bean名称匹配??有人能澄清我的疑问吗?参数名应该与方法名相同(因为方法名默认用作bean名),这样spring就可以自动关联依赖项。如果这种方法不起作用,您可以这样尝试:
@Bean
Binding bindingExchangeFoo() {
return BindingBuilder.bind(queueFoo()).to(exchange()).with("queue.foo");
}
@Bean
Binding bindingExchangeBar() {
return BindingBuilder.bind(queueBar()).to(exchange()).with("queue.bar");
}
此外,我还学习了教程-