Springboot 项目普通类调用 Mapper 接口使用 MybatisPlus 报错:空指针异常(NullPointerException)
Springboot 项目普通类调用 Mapper 接口使用 MybatisPlus 报错:空指针异常(NullPointerException)
报错
开发时,在普通类调用 Mapper 接口使用 MabatisPlus 功能时会报出【空指针异常( NullPointerException )】
分析
普通类调用Mapper:
- @Component 注册该普通Java类
- @Autowired 注册 Mapper 层接口
- 声明该类自身的静态类变量
- 用 @PostConstruct 注入 bean,声明 init() 方法,进行初始化挂载;
- 使用声明的该类的静态类变量,调用 Mapper 层
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
解决
方案一:
代码:
@Slf4j @NoArgsConstructor @Component public class RawMessageHandler extends AbstractMessageHandler { @Autowired private MessageMapper messageMapper; private static RawMessageHandler rawMessageHandler; @PostConstruct private void init(){ rawMessageHandler = this; rawMessageHandler.messageMapper = this.messageMapper; } @Override public boolean doHandle(ReturnMessage message) { MessageDO messageDO = new MessageDO(); messageDO.setIgwId(message.ID); messageDO.setSendAt(DateTimeUtils.string2DateTime(message.MessageUTC)); rawMessageHandler.messageMapper.insert(messageDO); return false; } }
方案二:
1、首先打印出Spring容器所有的Bean名称
@Component public class ApplicationContextBean implements ApplicationContextAware, InitializingBean { public static ApplicationContext applicationContext; /** * 获取 ApplicationContext * * @param applicationContext * @throws BeansException */ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { ApplicationContextBean.applicationContext = applicationContext; } /** * 打印IOC容器中所有的Bean名称 * * @throws Exception */ @Override public void afterPropertiesSet() throws Exception { String[] names = applicationContext.getBeanDefinitionNames(); for (String name : names) { System.out.println(">>>>>>" + name); } System.out.println("------\nBean 总计:" + applicationContext.getBeanDefinitionCount()); } }
2、开始写获取bean工具类
@Component public class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext = null; public SpringUtil() { } public void setApplicationContext(ApplicationContext arg0) throws BeansException { if (applicationContext == null) { applicationContext = arg0; } } public static ApplicationContext getApplicationContext() { return applicationContext; } public static void setAppCtx(ApplicationContext webAppCtx) { if (webAppCtx != null) { applicationContext = webAppCtx; } } /** * 拿到ApplicationContext对象实例后就可以手动获取Bean的注入实例对象 */ public static <T> T getBean(Class<T> clazz) { return getApplicationContext().getBean(clazz); } public static <T> T getBean(String name, Class<T> clazz) throws ClassNotFoundException { return getApplicationContext().getBean(name, clazz); } public static final Object getBean(String beanName) { return getApplicationContext().getBean(beanName); } public static final Object getBean(String beanName, String className) throws ClassNotFoundException { Class clz = Class.forName(className); return getApplicationContext().getBean(beanName, clz.getClass()); } public static boolean containsBean(String name) { return getApplicationContext().containsBean(name); } public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return getApplicationContext().isSingleton(name); } public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { return getApplicationContext().getType(name); } public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { return getApplicationContext().getAliases(name); } //测试 public static String getCharId(String chatId) { ChatListAssociationMapper associationMapper = (ChatListAssociationMapper) SpringUtil.getBean("chatListAssociationMapper");//这个参数是spring容器里存在的,名字最好复制,大小写要一致,不然会失败 return associationMapper.chatId(chatId); } }