diff --git a/luoo_music/src/main/java/com/luoo/music/config/RabbitMQConfig.java b/luoo_music/src/main/java/com/luoo/music/config/RabbitMQConfig.java index ff5751a..3da8250 100644 --- a/luoo_music/src/main/java/com/luoo/music/config/RabbitMQConfig.java +++ b/luoo_music/src/main/java/com/luoo/music/config/RabbitMQConfig.java @@ -1,12 +1,12 @@ package com.luoo.music.config; +import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; +import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; -import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import javax.annotation.Resource; - /** * @Author: yawei.huang * @Package: com.macro.mall.config @@ -16,20 +16,20 @@ import javax.annotation.Resource; * @Describe: */ @Configuration -public class RabbitMQConfig implements InitializingBean { +public class RabbitMQConfig { - /** - * 自动注入RabbitTemplate模板 - */ - @Resource - private RabbitTemplate rabbitTemplate; + @Bean + public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { + RabbitTemplate template = new RabbitTemplate(connectionFactory); + template.setMessageConverter(new Jackson2JsonMessageConverter()); + return template; + } - /** - * 发送消息JSON序列化 - */ - @Override - public void afterPropertiesSet() { - //使用JSON序列化 - rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter()); + @Bean + public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) { + SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); + factory.setConnectionFactory(connectionFactory); + factory.setMessageConverter(new Jackson2JsonMessageConverter()); + return factory; } }