SSM移到新类中再实例化后调用报空指针异常

水晶圣教士 2020-01-14 09:29:04
原正常代码:
@Component
public class TimerTaskImpl {
@Autowired
private OrderDao orderDao; //D

@Scheduled(cron = "0 0/1 * * * ?")
public void Task1() {

List<Order> listOrderLoans = new ArrayList<Order>(); //A
listOrderLoans = orderDao.getOrderPending_Loans(); //B
logger.debug("listOrderLoans#"+listOrderLoans.size()); //C

......
}
}

新类中的代码:

@Component
public class TaskLoans {
@Autowired
private OrderDao orderDao;

public void process() {
List<Order> listOrderLoans = new ArrayList<Order>();
listOrderLoans = orderDao.getOrderPending_Loans();
logger.debug("listOrderLoans#"+listOrderLoans.size());
...
}
}


调用的代码:
		TaskLoans tl = new TaskLoans();
tl.process();

SSM框架,做了一个定时任务,以上代码可以正常运行,可是我把A、B、C、D段代码移到一个新建类的方法中,通过实例化后调用,执行到代码B时就报java.lang.NullPointerException,单步调试发现移到新类后,orderDao空了,这是什么原因?是不是我哪里注解少了?谢谢。

后面是报错日志:
[2020-01-14 09:42:00,287][ERROR][org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler][scheduler-1](TaskUtils.java:95)[Unexpected error occurred in scheduled task.]
java.lang.NullPointerException
at com.xsrcc.ssmcxf.autotask.TaskLoans.process(TaskLoans.java:28)
at com.xsrcc.ssmcxf.autotask.TimerTaskImpl.Task1(TimerTaskImpl.java:106)
at com.xsrcc.ssmcxf.autotask.TimerTaskImpl$$FastClassBySpringCGLIB$$6f96cc76.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:266)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at com.xsrcc.ssmcxf.autotask.TimerTaskImpl$$EnhancerBySpringCGLIB$$84a22122.Task1(<generated>)
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:483)
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(FutureTask.java:266)
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:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
...全文
211 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
水晶圣教士 2020-01-16
  • 打赏
  • 举报
回复
最后没来得及使用构造器注入尝试,先用了一个笨办法,把对象OrderDao和BankOutletsDao作为参数传入使用,后期再尝试优化。谢谢!
苏暮沉觞 2020-01-14
  • 打赏
  • 举报
回复
在运行B时,对应的Bean还没注入?尝试一下构造器注入,而不是使用@Autowired

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧