java 空指针异常 排查_项目启动spring莫名爆null空指针异常,排查

项目之前跑起来运行好好的,结果 其他相关的模块更新代码之后,项目启动报错了

2019-12-06 08:55:13 [main] ERROR o.s.boot.SpringApplication - Application startup failed

java.lang.NullPointerException: null

at org.springframework.beans.factory.support.DefaultListableBeanFactory$FactoryAwareOrderSourceProvider.getOrderSource(DefaultListableBeanFactory.java:1742)

at org.springframework.core.OrderComparator.getOrder(OrderComparator.java:102)

at org.springframework.core.OrderComparator.doCompare(OrderComparator.java:87)

at org.springframework.core.OrderComparator.access$000(OrderComparator.java:48)

at org.springframework.core.OrderComparator$1.compare(OrderComparator.java:66)

at java.util.TimSort.countRunAndMakeAscending(TimSort.java:360)

at java.util.TimSort.sort(TimSort.java:234)

at java.util.Arrays.sort(Arrays.java:1438)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveMultipleBeans(DefaultListableBeanFactory.java:1171)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1096)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)

at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)

at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)

at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)

at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)

at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)

at com.newpearl.da.web.DaWebApplication.main(DaWebApplication.java:27)

2019-12-06 08:55:13 [Thread-8] ERROR c.x.j.c.thread.TriggerCallbackThread - null

java.lang.InterruptedException: null

at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:2014)

at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2048)

at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)

at com.xxl.job.core.thread.TriggerCallbackThread$1.run(TriggerCallbackThread.java:65)

也没有说是 代码哪里爆出来的, 直接就是 spring的 bean 获取然后就是order 比较哪些方法了。

表示看不太明白。 而 更新的模块代码 又比较多, 一个一个去找来看有没有问题?这 怎么搞?

最后的方式是可以 debug 到 出差的代码,但是要debug 到 spring 允许出错的代码,也不容易。

如何 debug 到 spring 执行 出错的地方?

我采用比较笨的方法。

首先 统计从 spring boot main方法启动 到报错 执行的时间, 大概知道项目启动

到报错 耗时多少。

之后在  初始化某个bean的时候, 新建一个 线程去 跑,

看 线程执行到什么时间 然后 才会报错。

之后 debug ,打上断点 。 接着给 spring 执行的代码 也打上断点

b0f2c95b999edb4e876b70e92262e2eb.png

比如 程序 开始启动允许了 30S之后 报错,就让 子线程休眠30秒,之后就可以进入断点了,去调试了

接着debug  spring 代码

1f8bd6f629156e407484349258cfeeff.png

这里进行调试会比较 费时费力, 因为 只有当  o1 为空的时候,才会报错。

所以需要一直按 F9进行调试到下一步 直到  o1 发现为空的时候,

这里不能 取巧,鬼知道我按了多少下  F9

终于 到了 o1 为空的时候, 这里 通过idea debug 左侧的 执行堆栈。

发现是在 初始化bean

AddCouponCodeTask

接着 下一步执行,发现 比较的两个对象, 一个是 bean  addCouponCodeTask  ,

另一个是

cloudSolrClient

发现 addCouponCodeTask  bean 是  null 的

@Deprecated

@Component

public class AddCouponCodeTask extends Thread {

其他同事 写的。。。 ,

在Spring项目中,有时需要新开线程完成一些复杂任务,而线程中可能需要注入一些服务。而通过Spring注入来管理和使用服务是较为合理的方式。但是若直接在Thread子类中通过注解方式注入Bean是无效的。

因为Spring本身默认Bean为单例模式构建,同时是非线程安全的,因此禁止了在Thread子类中的注入行为,因此在Thread中直接注入的bean是null的,会发生空指针错误。

好吧,我也是才知道 不能这样写的。

而 cloudSolrClient 为空, 我是知道的。我 写的时候为了防止  报错 ,因此给其 设置为空了。

因为 其他模块 代码里面 注入了   cloudSolrClient , 但是 整个项目并没有用到 solr 所以才 写的空

@Bean(name = "cloudSolrClient")

public CloudSolrClient setSolrClientFactory() {

return null;

将 addCouponCodeTask  代码全注释掉, 这次 启动成功了。

或者将  cloudSolrClient 代码, 默认 return 一个 新对象, 也可以启动成功了。

也就是 spring 在  初始化bean的时候, 会对 bean的 依赖 到的其他 bean 进行 排序比较,

当  orderComparator 执行比较的时候两个都是 null 的时候,就会出现这个问题了。

总结

1.  线程的 子类 不能 注入 spring 中, 否则是为 null 的

2.  写代码的时候, 尽量不要 为 bean 初始化和赋值 为空 , 否则可能出现 空指针的 莫名其妙的 坑

文章来源: www.oschina.net,作者:之渊,版权归原作者所有,如需转载,请联系作者。

原文链接:https://my.oschina.net/ouminzy/blog/3138251

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值