spring 标签

 */
@Slf4j
@Service
public class RetryService {

    @Autowired
    private MqConfig mqConfig;

    /**
     * 如果网络连接失败,数据会发送到MQ
     * 前台监听此MQ
     *
     * @param cityRequest
     */
    public  void sendRetryMsg(CityRequest cityRequest) {

我在使用RetryService的时候,这样使用:是争取的

方法1:

    @Autowired
    private RetryService retryService; 

            for (CityRequest cityRequest : cityRequestList) {
                retryService.sendRetryMsg(cityRequest);
            }

但是这样:是不对的;

方法2:

RetryService retryService =new RetryService();  
 for (CityRequest cityRequest : cityRequestList) {
                retryService.sendRetryMsg(cityRequest);
            }

因为,方法2中,没有使用spring注入;

直接new了一个类,但是new 了一个新的实例之后,这个类里面的

  @Autowired
    private MqConfig mqConfig;

需要从新注入才行,不然运行到需要mqConfig的时候,会报错:空指针异常的;
原文地址:https://www.cnblogs.com/aspirant/p/10405786.html