新人踩坑的一天——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项目时的确是通过 ...
随机推荐
- 2019-10-31:渗透测试,基础学习,mongodb注入学习
mongodb命令删除文档,db.集合名.remove({键:值})更新文档,db.集合名.update({被更新的对象},{$set:{要更新的内容}}),默认只更新检测到的第一条文档更新全部匹配到 ...
- 新闻实时分析系统-MySQL安装
1.修改yum源 鉴于用国外的Yum源,速度比较慢,所以想到将国外的yum源改为国内的Yum源,这里选择使用比较多的阿里云源.具体修改方法可以参考此连接 2.在线安装mysql 通过yum在线mysq ...
- PL真有意思(七):数据抽象和面向对象
前言 在之前的名字.作用域那篇提到模块类型,它使程序员可以从一个给定抽象出发,通过实例化产生多个实例:再后面是类,它使程序员可以定义一族相关的抽象. 在这一篇里,我们会来看一下面向对象程序设计及其三个 ...
- C语言基础 -- 变量
常用变量类型 地址 小端 低地址保存低位,高地址保存高位 常用于 PC(复杂指令集) 大端 低地址保存高位,高地址保存低位 常用于 ARM/手机/网络(精简指令集)
- Java基础IO类之对象流与序列化
对象流的两个类: ObjectOutputStream:将Java对象的基本数据类型和图形写入OutputStream ObjectInputStream:对以前使用ObjectOutputStrea ...
- docker下安装测试环境estuntest
1.基础知识: docker pull centos //从云上下载centos系统到本地服务器 docker images //查看镜像docker rmi 镜像id //删除镜像 do ...
- 用JS实现HTML转PDF
遇到这个需求,现把实现代码整理出来,方便大家参考 <!-- html转PDF --> <script src="https://cdnjs.cloudflare.com/a ...
- [TimLinux] Django 中间件
1. 定义 中间件是一个钩子框架,深入到django的请求/响应处理过程中.这是一个轻量.底层插件系统,目的是全局修改django的输入或输出.每一个中间件组件都是用来处理特定的功能.例如django ...
- LightOJ 1229 Tablecross
Treblecross is a two player game where the goal is to get three X in a row on a one-dimensional boar ...
- POJ 2559 Langest Rectangle in a Histogame
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...