Interceptor 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对象
抛出异常的原因
所以整理下整个分析:
- 在使用了@Transactional之后,spring aop会生成一个cglib代理类,实际用户代码里@Autowired注入的StudentDao也是这个代理类的实例
- cglib生成的代理类StudentDao$$EnhancerBySpringCGLIB$$210b005d继承自StudentDao
- StudentDao$$EnhancerBySpringCGLIB$$210b005d里的所有field都是null
- StudentDao$$EnhancerBySpringCGLIB$$210b005d在调用selectStudentById,实际上通过CglibAopProxy$DynamicAdvisedInterceptor,最终会用反射调用自己保存的原始target对象
- 所以selectStudentById函数的调用没有问题
那么为什么finalSelectStudentById函数里的SqlSession sqlSession会是null,然后抛出NullPointerException?
- StudentDao$$EnhancerBySpringCGLIB$$210b005d里的所有field都是null
- finalSelectStudentById函数的修饰符是final,cglib没有办法重写这个函数
- 当执行到finalSelectStudentById里,实际执行的是原始的StudentDao里的代码
- 但是对象是StudentDao$$EnhancerBySpringCGLIB$$210b005d的实例,它里面的所有field都是null,所以会抛出NullPointerException
解决问题办法
- 最简单的当然是把finalSelectStudentById函数的final修饰符去掉
- 还有一种办法,在StudentDao里不要直接使用sqlSession,而通过getSqlSession()函数,这样cglib也会处理getSqlSession(),返回原始的target对象
总结
- 排查问题多debug,看实际运行时的对象信息
- 对于cglib生成类的字节码,可以用dumpclass工具来dump,再反编绎分析