package com.sf.mq.rabbitmq.topic; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class TopicConfig { // 通过bean的声明方式 来创建交换器 // 声明交换器时 只需要类型和名字 直接使用对应类型的实现类FanoutExchange // 通过构造器指定名字 @Bean public TopicExchange topicExchange() { return new TopicExchange("lovecoding.topic1"); } // 通过bean的声明方式 来创建队列 // 通过队列的构造器指定名字 // 注意引用的包来自 org.springframework.amqp.core @Bean public Queue topicQueue() { return new Queue("topic.queue1"); } // 绑定交换器和队列 // 当前是fanout类型 不需要routingKey // 绑定关系叫做Binding类 通过BindingBuilder来构造绑定关系 // bind方法后面加队列 再使用链式编程 to方法后面加交换器 @Bean public Binding bindingTopic(Queue topicQueue, TopicExchange topicExchange) { return BindingBuilder.bind(topicQueue).to(topicExchange).with("*.*.harbin"); } }