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的时候,会报错:空指针异常的;
posted @   aspirant  阅读(195)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2014-02-20 二叉树例子
2014-02-20 二叉树 排序二叉树-可以通过中序遍历得到排序的数据 二叉排序树时间复杂度O(logn),
点击右上角即可分享
微信分享提示