设置rabbitTemplate和controller的scope都为prototype即可
@Bean
//设置rabbitTemplate的scope为:prototype
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public RabbitTemplate noSingleRabbitTemplate() throws IOException, TimeoutException {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connection());
//设置消息进入交换机后未被队列接收的消息不被丢弃由broker保存,flase为丢弃
rabbitTemplate.setMandatory(true);
rabbitTemplate.setReceiveTimeout(30000);
rabbitTemplate.setReplyTimeout(30000);
return rabbitTemplate;
}
加粗样式
@RestController
//设置controller 的scope为:prototype
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@RequestMapping("/sendMessage/")
public class SendMessageController {
@Resource(name = "noSingleRabbitTemplate")
private RabbitTemplate noSingleRabbitTemplate;
}