2017-07-28 51 views
2

我正在尝试使用Spring创建调度程序。用Spring创建调度程序Bean时发生NullPointerException

@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和春天版本4.3.6,但与Ant和没有依赖管理,因此缺少库或库不匹配是一种可能性。

+0

它是一个Java程序和一个来自标准库的异常,我认为这个标记足够了!? – schrobe

+0

我无法在没有Java的情况下重现它 – schrobe

+0

继续吧,标记它java,键盘,鼠标,显示器,电,眼睛,大脑......服务器正在启动,spring正在初始化,jsf还没有做任何事情。 – Kukeltje

回答

1

刚走了同样的错误在科特林应用,我的解决方法是手动将TaskScheduler添加到我的应用程序配置:

Java的例子是采取from here

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

我科特林版本

@Bean 
fun threadPoolTaskScheduler() 
    = ThreadPoolTaskScheduler().apply { 
     setPoolSize(5) 
     setThreadNamePrefix("ThreadPoolTaskScheduler") 
} 

(我还没有尝试过,但我相信升级到弹簧5也将解决这个问题 - this commit抛出一个异常的空指针会一直生成。)

相关问题