深入学习Spring Boot排查 @Transactional 引起的 NullPointerException问题

 更新时间:2018年01月11日 12:07:13   作者:ImportNew  
这篇文章主要介绍了深入学习Spring Boot排查 @Transactional 引起的 NullPointerException问题,需要的朋友可以参考下

写在前面

这个demo来说明怎么排查一个@Transactional引起的NullPointerException。

https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-Transactional-NullPointerException

定位 NullPointerException 的代码

Demo是一个简单的spring事务例子,提供了下面一个StudentDao,并用@Transactional来声明事务:

@Component
@Transactional
public class StudentDao {
 @Autowired
 private SqlSession sqlSession;
 public Student selectStudentById(long id) {
  return sqlSession.selectOne("selectStudentById", id);
 }
 public final Student finalSelectStudentById(long id) {
  return sqlSession.selectOne("selectStudentById", id);
 }
}

应用启动后,会依次调用selectStudentById和finalSelectStudentById:

@PostConstruct
 public void init() {
  studentDao.selectStudentById(1);
  studentDao.finalSelectStudentById(1);
 }

用mvn spring-boot:run 或者把工程导入IDE里启动,抛出来的异常信息是:

Caused by: java.lang.NullPointerException
 at sample.mybatis.dao.StudentDao.finalSelectStudentById(StudentDao.java:27)
 at com.example.demo.transactional.nullpointerexception.DemoNullPointerExceptionApplication.init(DemoNullPointerExceptionApplication.java:30)
 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.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
 at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)

为什么应用代码里执行selectStudentById没有问题,而执行finalSelectStudentById就抛出NullPointerException?

同一个bean里,明明SqlSession sqlSession已经被注入了,在selectStudentById里它是非null的。为什么finalSelectStudentById函数里是null?

获取实际运行时的类名

当然,我们对比两个函数,可以知道是因为finalSelectStudentById的修饰符是final。但是具体原因是什么呢?

我们先在抛出异常的地方打上断点,调试代码,获取到具体运行时的class是什么:

System.err.println(studentDao.getClass());

打印的结果是:

class sample.mybatis.dao.StudentDao$$EnhancerBySpringCGLIB$$210b005d

可以看出是一个被spring aop处理过的类,但是它的具体字节码内容是什么呢?

dumpclass分析

我们使用dumpclass工具来把jvm里的类dump出来:

https://github.com/hengyunabc/dumpclass

wget http://search.maven.org/remotecontent?filepath=io/github/hengyunabc/dumpclass/0.0.1/dumpclass-0.0.1.jar -O dumpclass.jar

找到java进程pid:

$ jps
5907 DemoNullPointerExceptionApplication

把相关的类都dump下来:

sudo java -jar dumpclass.jar 5907 'sample.mybatis.dao.StudentDao*' /tmp/dumpresult

反汇编分析

用javap或者图形化工具jd-gui来反编绎sample.mybatis.dao.StudentDao$$EnhancerBySpringCGLIB$$210b005d。

反编绎后的结果是:

class StudentDao$$EnhancerBySpringCGLIB$$210b005d extends StudentDao

StudentDao$$EnhancerBySpringCGLIB$$210b005d里没有finalSelectStudentById相关的内容

selectStudentById实际调用的是this.CGLIB$CALLBACK_0,即MethodInterceptor tmp4_1,等下我们实际debug,看具体的类型

public final Student selectStudentById(long paramLong)
 {
 try
 {
  MethodInterceptor tmp4_1 = this.CGLIB$CALLBACK_0;
  if (tmp4_1 == null)
  {
  tmp4_1;
  CGLIB$BIND_CALLBACKS(this);
  }
  MethodInterceptor tmp17_14 = this.CGLIB$CALLBACK_0;
  if (tmp17_14 != null)
  {
  Object[] tmp29_26 = new Object[1];
  Long tmp35_32 = new java/lang/Long;
  Long tmp36_35 = tmp35_32;
  tmp36_35;
  tmp36_35.<init>(paramLong);
  tmp29_26[0] = tmp35_32;
  return (Student)tmp17_14.intercept(this, CGLIB$selectStudentById$0$Method, tmp29_26, CGLIB$selectStudentById$0$Proxy);
  }
  return super.selectStudentById(paramLong);
 }
 catch (RuntimeException|Error localRuntimeException)
 {
  throw localRuntimeException;
 }
 catch (Throwable localThrowable)
 {
  throw new UndeclaredThrowableException(localThrowable);
 }
 }

再来实际debug,尽管StudentDao$$EnhancerBySpringCGLIB$$210b005d的代码不能直接看到,但是还是可以单步执行的。

在debug时,可以看到

1. StudentDao$$EnhancerBySpringCGLIB$$210b005d里的所有field都是null

 

2. this.CGLIB$CALLBACK_0的实际类型是CglibAopProxy$DynamicAdvisedInterceptor,在这个Interceptor里实际保存了原始的target对象

 

3. CglibAopProxy$DynamicAdvisedInterceptor在经过TransactionInterceptor处理之后,最终会用反射调用自己保存的原始target对象

抛出异常的原因

所以整理下整个分析:

1.在使用了@Transactional之后,spring aop会生成一个cglib代理类,实际用户代码里@Autowired注入的StudentDao也是这个代理类的实例

2.cglib生成的代理类StudentDao$$EnhancerBySpringCGLIB$$210b005d继承自StudentDao

3.StudentDao$$EnhancerBySpringCGLIB$$210b005d里的所有field都是null

4.StudentDao$$EnhancerBySpringCGLIB$$210b005d在调用selectStudentById,实际上通过CglibAopProxy$DynamicAdvisedInterceptor,最终会用反射调用自己保存的原始target对象

5.所以selectStudentById函数的调用没有问题

那么为什么finalSelectStudentById函数里的SqlSession sqlSession会是null,然后抛出NullPointerException?

1.StudentDao$$EnhancerBySpringCGLIB$$210b005d里的所有field都是null

2.finalSelectStudentById函数的修饰符是final,cglib没有办法重写这个函数

3.当执行到finalSelectStudentById里,实际执行的是原始的StudentDao里的代码
4.但是对象是StudentDao$$EnhancerBySpringCGLIB$$210b005d的实例,它里面的所有field都是null,所以会抛出NullPointerException

解决问题办法

1.最简单的当然是把finalSelectStudentById函数的final修饰符去掉

2.还有一种办法,在StudentDao里不要直接使用sqlSession,而通过getSqlSession()函数,这样cglib也会处理getSqlSession(),返回原始的target对象

总结

1.排查问题多debug,看实际运行时的对象信息

2。对于cglib生成类的字节码,可以用dumpclass工具来dump,再反编绎分析

总结

以上所述是小编给大家介绍的深入学习Spring Boot排查 @Transactional 引起的 NullPointerException问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • Java集合的Collection接口和List接口详解

    Java集合的Collection接口和List接口详解

    这篇文章主要为大家详细介绍了Java集合的Collection接口和List接口,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair 解决方法总结

    javax.net.ssl.SSLException: java.lang.RuntimeException: Coul

    这篇文章主要介绍了javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair 解决方法,有需要的朋友们可以学习下。
    2019-08-08
  • sun unsafe类功能及使用注意事项详解

    sun unsafe类功能及使用注意事项详解

    这篇文章主要为大家介绍了unsafe类的功能及在使用中需要注意的事项详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-01-01
  • 浅谈hibernate中多表映射关系配置

    浅谈hibernate中多表映射关系配置

    下面小编就为大家带来一篇浅谈hibernate中多表映射关系配置。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • Java模板动态生成word文件的方法步骤

    Java模板动态生成word文件的方法步骤

    最近项目中需要根据模板生成word文档,模板文件也是word文档。本文使用使用freemarker模板生成word文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • Log4net 日志记录详细介绍及应用

    Log4net 日志记录详细介绍及应用

    这篇文章主要介绍了Log4net 日志记录详细介绍及应用的相关资料,需要的朋友可以参考下
    2017-02-02
  • Mybatis-plus批量插入的2种方式总结

    Mybatis-plus批量插入的2种方式总结

    这篇文章主要给大家总结介绍了关于Mybatis-plus批量插入的2种方式,Mybatis-Plus提供了多种方式进行批量插入优化,文中通过代码示例将实现的方法介绍的非常详细,需要的朋友可以参考下
    2023-08-08
  • Flink自定义Sink端实现过程讲解

    Flink自定义Sink端实现过程讲解

    这篇文章主要介绍了Flink自定义Sink端实现过程,在Fink官网中sink端只是给出了常规的write api.在我们实际开发场景中需要将flink处理的数据写入kafka,hbase kudu等外部系统
    2023-01-01
  • Java在算法题中的输入问题实例详解

    Java在算法题中的输入问题实例详解

    在校招笔试中,有的时候我们要自己设计输入输出,所以下面这篇文章主要给大家介绍了关于Java在算法题中的输入问题,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2022-02-02
  • 用Java集合中的Collections.sort方法如何对list排序(两种方法)

    用Java集合中的Collections.sort方法如何对list排序(两种方法)

    本文通过两种方法给大家介绍java集合中的Collections.sort方法对list排序,第一种方式是list中的对象实现Comparable接口,第二种方法是根据Collections.sort重载方法实现,对collections.sort方法感兴趣的朋友一起学习吧
    2015-10-10

最新评论