新人踩坑的一天——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自己完成创建和装配,才解决了问题。不复杂,但是网上很多地方的答案不是需要的,问题定位由于缺乏经验还是花了些时间(哈哈
新人踩坑的一天——springboot注入mapper时出现java.lang.NullPointerException: null的更多相关文章
- SpringBoot注入Mapper失败
SpringBoot注入Mapper失败,可能是因为没有加扫描Mapper层的注解 方式一:在所有mapper接口使用@Mapper注解 @Mapper public interface UserMa ...
- SpringBoot项目意外出现 循环依赖和注入的对象意外是Null的问题 Requested bean is currently in creation: Is there an unresolvable circular reference? 或 nested exception is java.lang.NullPointerException
1.Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ...
- springboot测试启动报错java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
springboot测试启动报错: java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you ne ...
- 踩坑了!使用 @Autowired 注入成功,GetBean 方法却获取不到?!
本文首发于个人微信公众号:Coder小黑 踩坑了?! 之前推文已经讲过 当@Transactional遇到@CacheEvict,你的代码是不是有bug! 现在要在事务提交之后清除缓存.在Spring ...
- SpringBoot测试类启动错误 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
报错 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Cont ...
- springboot 整合 CXF 版本异常 java.lang.NoClassDefFoundError:ServletRegistrationBean
在使用SpringBoot 项目整合webservice组件 CXF的时候,在启动时,抛出异常如下,查阅资料初步判断为版本问题.升级到高版本后正常启动. cxf 刚开始使用版本 3.1.7 后更新为 ...
- 解决 spring boot 线程中使用@Autowired注入Bean的方法,报java.lang.NullPointerException异常
问题描述 在开发中,因某些业务逻辑执行时间太长,我们常使用线程来实现.常规服务实现类中,使用 @Autowired 来注入Bean,来调用其中的方法.但如果在线程类中使用@Autowired注入的Be ...
- 四、SpringBoot出现报错:java.lang.NoSuchMethodError: org.springframework.http.MediaType.equalsTypeAndSubtype(Lorg/springframework/util/MimeType;)Z
idea启动SpringBoot项目后,出现如下错误: 2019-11-19 15:24:44.344 ERROR 39168 --- [nio-8443-exec-1] o.a.c.c.C.[.[. ...
- 【踩坑】服务器部署springboot应用时报错--端口被tomcat占用
今天将本机尬聊一下项目(基于netty-socketio)的服务端程序调试好以后,通过jar包部署在服务器的时候,出现了报错,提示tomcat已经占用了端口. 之前在部署iReview项目时的确是通过 ...
随机推荐
- day 14 内置函数
复习了解: \t 输出一个制表符,协助在输出文本时,垂直方向保持对齐 \n 换行符 print(r"\n ") # 在字符串前面加r 不会改变字符串的内容 a ...
- kubeadm 报错 error execution phase preflight: couldn’t validate the identity of the API Server: abort connecting to API servers after timeout of 5m0s
原因:master节点的token过期了 解决:重新生成新token 在master重新生成token # kubeadm token create 424mp7.nkxx07p940mkl2nd # ...
- block中self会造成循环引用问题
将代码块中的 self换成unsafeSelf __unsafe_unretained 与 __weak 99%相同 __weak 当对象释放之后 会自动设置为nil 而__unsafe_unreta ...
- PAT(甲级)2018年冬季考试
1152 Google Recruitment 思路:判断素数 #include<bits/stdc++.h> using namespace std; const int maxn = ...
- 记录我的 python 学习历程-Day03 数据类型 str切片 for循环
一.啥是数据类型 我们人类可以很容易的分清数字与字符的区别,但是计算机并不能呀,计算机虽然很强大,但从某种角度上看又很傻,除非你明确的告诉它,1是数字,"汉"是文字,否则它是分 ...
- netty的调优-及-献上写过注释的源码工程
Netty能干什么? Http服务器 使用Netty可以编写一个 Http服务器, 就像tomcat那样,能接受用户发送的http请求, , 只不过没有实现Servelt规范, 但是它也能解析携带的参 ...
- Java判断字符串相等"=="和"equal"详解
在初学Java时,可能会经常碰到下面的代码: public static void main(String[] args) { //两种声明方式,有所差别 String s1="hello& ...
- 判断一个坐标点是否在封闭曲线内的方法(swift)
//用playground运行即可 import UIKit var str = "Hello, playground" let lTestPointsOne : [(Double ...
- DevOps on DevCloud|如何实现应用接口的混合驱动测试
引言:在"DevOps能力之屋(Capabilities House of DevOps)"中,华为云DevCloud提出(工程方法+最佳实践+生态)×工具平台=DevOps能力. ...
- luogu P3984 高兴的津津
题目描述 津津上高中了.她在自己的妈妈的魔鬼训练下,成为了一个神犇,每次参加一次OI比赛必拿Au虐全场.每次她拿到一个Au后就很高兴.假设津津不会因为其它事高兴,并且她的高兴会持续T天(包包含获奖当天 ...