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