rabbitTemplate NPE

在使用大华SDK进行二次开发时,在回调函数尝试通过rabbitTemplate发送MQ消息时遇到空指针异常。问题源于未加注解导致类未被Spring容器管理,@Autowired无法生效。解决方法是使类加入Spring容器,例如使用@Component或其他注解,并通过Spring容器获取bean。
摘要由CSDN通过智能技术生成

rabbitTemplate NPE(空指针异常)

问题

用大华SDK二次开始的时候,想在回调函数里发送消息给MQ,注入rabbitTemplate时产生异常。

原因

如果一个类没有加注解如@component, @controller, @service等扫描这个类到容器中
在类中的变量加@Autowired注解无法生效。
因为如果一个类new对象生成的,那么这个类就不归spring容器管理,IOC等spring的功能也就无法使用了。

解决方法

普通类获取Spring容器中的bean
可以参考:https://www.cnblogs.com/s648667069/p/6489557.html

package com.gmy.AccessCTLManagementSys.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 *
 * 普通类调用Spring bean对象:
 *
 * @authon GMY
 * @create 2020-10-23 17:40
 */
@Component
public class SpringUtil im
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值