跳至主要內容

dao依赖注入空指针异常

chanchaw大约 1 分钟java

概述

报错提示

查看日志文件报错的最后一个错误提示如下:

Caused by: java.lang.NullPointerException: null
	at com.xdf.showa.service.InspectPlanServiceImpl.getDistGenerateTime(InspectPlanServiceImpl.java:214)
	at com.xdf.showa.service.InspectPlanServiceImpl$$FastClassBySpringCGLIB$$dea8ecbc.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
	at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:64)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
	at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:89)
	at com.xdf.showa.aspect.DynamicDataSourceAspect.aroundLocal(DynamicDataSourceAspect.java:96)
	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.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:634)
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:624)
	at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:72)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708)
	at com.xdf.showa.service.InspectPlanServiceImpl$$EnhancerBySpringCGLIB$$aeb052bc.getDistGenerateTime(<generated>)
	at com.xdf.showa.service.InspectPlanSchedule.createSchedule(InspectPlanSchedule.java:39)
	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:389)
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333)
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157)
	... 102 common frames omitted

找到源码 InspectPlanServiceImpl.getDistGenerateTime(InspectPlanServiceImpl.java:214) 代码如下:

@Override
public List<InspectPlan> getDistGenerateTime(){
    return dao.getDistGenerateTime();
}

提示上面的 return dao.getDistGenerateTime(); 空指针异常,但是本类的头上已经使用了 @Autowired 指定了依赖注入

解决

问题出在手动制作的一个类 InspectPlanSchedule 中依赖注入了 InspectPlanServiceImpl 的服务接口 InspectPlanService 导致两者循环依赖同时没有使用注解 @Lazy

@Autowired
private InspectPlanService inspectPlanService;

InspectPlanServiceImpl 中的依赖注入(下面代码) 的头上使用懒加载注解即可解决

@Lazy
@Autowired
private InspectPlanSchedule inspectPlanSchedule;