使用Spring创建调度程序Bean时出现NullPointerException

时间:2017-07-28 16:43:47

标签: java spring cron quartz-scheduler

我正在尝试使用Spring创建一个调度程序。

@Configuration
@EnableScheduling
public class MyScheduler {


  @Autowired
  MyBusinessService businessService;


  @Scheduled(cron = "* * * * * *")
  public void myCronMethod() {

  }
}

在应用程序启动期间,我收到以下错误:

java.lang.NullPointerException
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.resolveSchedulerBean(ScheduledAnnotationBeanPostProcessor.java:281)
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:221)
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:200)
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:94)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:337)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:882)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5068)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5584)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1572)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1562)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    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)

ScheduledAnnotationBeanPostProcessor 中,持有人为空:

NamedBeanHolder<T> holder = ((AutowireCapableBeanFactory) this.beanFactory).resolveNamedBean(schedulerType);

schedulerType 是:

interface org.springframework.scheduling.TaskScheduler

该应用程序是一个JSF应用程序。我使用java 8和spring版本4.3.6,但是使用Ant并且没有依赖项管理,因此缺少库或库不匹配是可能的。

1 个答案:

答案 0 :(得分:1)

在Kotlin应用程序中遇到同样的错误,我的解决方法是手动将A = csr_matrix((data,(rowind,columnind)), shape=(200000, 150000), dtype=np.uint8) permutation_array = csgraph.reverse_cuthill_mckee(A, false) result_matrix = lil_matrix((200000, 150000), dtype=np.uint8) i=0 for x in np.nditer(permutation_array): result_matrix[x, :]=A[i, :] i+=1 添加到我的应用程序配置中:

Java示例是from here

TaskScheduler

我的Kotlin版本

@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler(){
    ThreadPoolTaskScheduler threadPoolTaskScheduler
      = new ThreadPoolTaskScheduler();
    threadPoolTaskScheduler.setPoolSize(5);
    threadPoolTaskScheduler.setThreadNamePrefix(
      "ThreadPoolTaskScheduler");
    return threadPoolTaskScheduler;
}

(我还没有尝试过,但我相信升级到Spring 5也会解决这个问题 - this commit抛出一个Exception,它会生成空指针。)