新人踩坑的一天——springboot注入mapper时出现java.lang.NullPointerException: null
来公司的第二周接到了定时任务的开发需求:每天早上十点发送用户报表邮件 。校招新人菜鸟没做过这玩意有些懵(尴尬)于是决定分步写,从excel导出->邮件发送->定时器实现->mapper层返回集接收,前几步都没啥问题,都在service层,可以用main方法单独测试下,可以发送邮件,但是通过service调用mapper时问题就来了——菜鸟踩坑了,抛出java.lang.nullpointerexception,下图是代码:
@component @enablescheduling @service("crontaskservice") public class crontaskserviceimpl implements crontaskservice { @resource private exportexcelserviceimpl exportexcelservice; @resource private mailsendserviceimpl mailsendservice; private final static logger log = loggerfactory.getlogger(crontaskserviceimpl.class); private static int count = 0; @scheduled(cron = "*/10 * * * * ?") @override public void out(){ new exportexcelserviceimpl().exportexcel("zhoubaobiao1"); //string filepath = exportexcelservice.exportexcel("周报表" + count++ + ".xls"); //mailsendservice.sendemail(filepath); log.info("-----------success-------\n"); } }
问了组内的前辈(应该也是没做几年),问题解决不了,定位不对。后面又在网上找了许久,在一位博主博文中找到了原因,链接:https://blog.csdn.net/itguangit/article/details/78305278
我的想法是通过new()方法创建exportexcelserviceimpl对象调用exportexcel()方法,exportexcel()方法中调用mapper层连接云上库,然后实现导出excel数据表到本地,结果就出现了空指针异常
[] 2019-12-19 13:22:20.003 [pool-1-thread-1] error o.s.s.support.taskutils$loggingerrorhandler - unexpected error occurred in scheduled task. java.lang.nullpointerexception: null at com.titansaas.cronjob.service.impl.exportexcelserviceimpl.exportexcel(exportexcelserviceimpl.java:116) at com.titansaas.cronjob.service.impl.crontaskserviceimpl.out(crontaskserviceimpl.java:36) at sun.reflect.nativemethodaccessorimpl.invoke0(native method) at sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) at sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) at java.lang.reflect.method.invoke(method.java:498) at org.springframework.scheduling.support.scheduledmethodrunnable.run(scheduledmethodrunnable.java:65) at org.springframework.scheduling.support.delegatingerrorhandlingrunnable.run(delegatingerrorhandlingrunnable.java:54) at org.springframework.scheduling.concurrent.reschedulingrunnable.run(reschedulingrunnable.java:81) at java.util.concurrent.executors$runnableadapter.call(executors.java:511) at java.util.concurrent.futuretask.run$$$capture(futuretask.java:266) at java.util.concurrent.futuretask.run(futuretask.java) at java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask.access$201(scheduledthreadpoolexecutor.java:180) at java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask.run(scheduledthreadpoolexecutor.java:293) at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1149) at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:624) at java.lang.thread.run(thread.java:748)
想法比较美好,但是初接触springboot不知道一个关键问题,new()出来的对象,无论是在对象或是在对象内部通过注解@resource或者是@autowired实现自动装配,或者是对方法进行@postconstruct标记(又挖坑坑自己,引发其他一系列问题),期望spring扫描到其中的mapper对象,都做不到;spring不会对其进行自动装配bean,注入mapper失败,导致mapper层映射无法完成,写好的xml中的sql无法执行。后来在上博文中找到了原因,重新通过@resource注解注册上面几个service层对象,让spring自己完成创建和装配,才解决了问题。不复杂,但是网上很多地方的答案不是需要的,问题定位由于缺乏经验还是花了些时间(哈哈