Spring中的TaskScheduler的NoSuchBeanDefinition

时间:2013-02-19 01:20:36

标签: spring scheduled-tasks

我正在尝试使用Spring计划任务。这是我的代码:

@Service
public class MyWork implements Runnable {
    @Override
    public void run() {
        workToDo();
    }


    private void workToDo()  { /*do it*/}
}

@Service
public class MySchedulerInvoker {
    @Autowired
    private TaskScheduler scheduler;

    @Async
    public void executeTask() {
    scheduler.schedule(new MyWork(), new CronTrigger(
            "* 15 9-17 * * MON-FRI"));
}

当我跑步时,我收到以下错误:

ava.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
[...]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name     'mySchedulerInvoker ': Injection of autowired dependencies failed; nested exception is     org.springframework.beans.factory.BeanCreationException: Could not autowire field: private     org.springframework.scheduling.TaskScheduler com.infoone.siglo.GestoreNotificheCtr.scheduler; nested exception is     org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type     [org.springframework.scheduling.TaskScheduler] found for dependency: expected at least 1 bean which qualifies as     autowire candidate for this dependency. Dependency annotations: [...]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private     org.springframework.scheduling.TaskScheduler com.infoone.siglo.GestoreNotificheCtr.scheduler; nested exception is     org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type     [org.springframework.scheduling.TaskScheduler] found for dependency: expected at least 1 bean which qualifies as     autowire candidate for this dependency. Dependency annotations: [...]

缺少什么?

1 个答案:

答案 0 :(得分:1)

在与TaskScheduler相同的上下文中,没有MySchedulerInvoker bean可用。您需要为某人显示一些配置以指出原因。