Day of New Man's Trench - java.lang.NullPointerException: null appears when springboot injects mapper

Keywords: Java Spring Excel SpringBoot

The next week I came to the company, I received a development request for a timed task: Send a user report mail every morning at 10:00 a.m.It's a little embarrassing that the new recruits didn't do this.) so decided to write step by step, export from excel - > mail send - > timer implementation - > mapper layer return set receive, the first few steps are no problem, in the service layer, you can use the main method to test separately, you can send mail, but the problem is when calling mapper through the service - the novice stepped on the pit, throwing out java.lang.NullPointerException, the following diagram is the code:

@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("Weekly Report" + Count++ + ".xls");

        //mailSendService.sendEmail(filePath);

    log.info("-----------success-------\n");

    }

}

Asked the elders in the group (should have not done for several years), the problem can not be solved, the positioning is not correct.After a long time online, I found the reason in a blogger's blog post, link: https://blog.csdn.net/itguangit/article/details/78305278

My idea is to create an ExportExcelServiceImpl object through the new() method, call the exportExcel() method, call the mapper layer in the exportExcel() method to connect to the cloud libraries, and then export the excel data tables locally, resulting in a null pointer exception

[] 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)

It's a nice idea, but new() doesn't know a key issue when first contacting springboot. Whether it's auto-assembling by annotating @Resource or @Autowired inside the object or marking the method @PostConstruct (digging yourself again, causing a series of other problems)), can't expect spring to scan mapper objects into it; spring will not automatically assemble bean s into it, and injection of mapper fails, resulting in mapper layer mapping not being completed, and SQL in written XML cannot be executed.Later, I found out why in my blog post, and resolved the problem by re-registering the above Service layer objects with the @Resource annotation and letting Spring create and assemble itself.It's not complicated, but answers aren't needed in many places on the web. It took some time (Haha) to locate the problem because of inexperience. If there's something wrong to ask, the apes learn ing...

Posted by kester on Thu, 19 Dec 2019 00:42:36 -0800