新人踩坑的一天——springboot注入mapper時出現java.lang.NullPointerException: null

來公司的第二週接到了定時任務的開發需求:天天早上十點發送用戶報表郵件 。校招新人菜鳥沒作過這玩意有些懵(尷尬)因而決定分步寫,從excel導出->郵件發送->定時器實現->mapper層返回集接收,前幾步都沒啥問題,都在service層,能夠用main方法單獨測試下,能夠發送郵件,可是經過service調用mapper時問題就來了——菜鳥踩坑了,拋出java.lang.NullPointerException,下圖是代碼:java

@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/78305278git

個人想法是經過new()方法建立ExportExcelServiceImpl對象調用exportExcel()方法,exportExcel()方法中調用mapper層鏈接雲上庫,而後實現導出excel數據表到本地,結果就出現了空指針異常spring

[] 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本身完成建立和裝配,才解決了問題。不復雜,可是網上不少地方的答案不是須要的,問題定位因爲缺少經驗仍是花了些時間(哈哈😄)若有不對請指教,程序猿學習ing...springboot

相關文章
相關標籤/搜索