SpringBoot3.0整合RocketMQ时出现未能加载bean文件
问题
APPLICATION FAILED TO START
Description:
Field rocketMQTemplate in com.spt.message.service.MqProducerService required a bean of type ‘org.apache.rocketmq.spring.core.RocketMQTemplate’ that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type ‘org.apache.rocketmq.spring.core.RocketMQTemplate’ in your configuration.
解决方案
定义一个配置类
@Configuration
public class RocketMqConfig {
@Value("${rocketmq.producer.group}")
private String producerGroup;
@Value("${rocketmq.name-server}")
private String nameServer;
/**
* 由于使用的Spring版本是3.0.0以上,与rocketMq不是很兼容,对于rocketMqTemplate
* 的自动注入存在差异,如果不采用这种方式注入则会报出缺少bean的信息
*/
@Bean("RocketMqTemplate")
public RocketMQTemplate rocketMqTemplate(){
RocketMQTemplate rocketMqTemplate = new RocketMQTemplate();
DefaultMQProducer defaultMqProducer = new DefaultMQProducer();
defaultMqProducer.setProducerGroup(producerGroup);
defaultMqProducer.setNamesrvAddr(nameServer);
rocketMqTemplate.setProducer(defaultMqProducer);
return rocketMqTemplate;
}
}
这样就可以成功运行了。