Spring中TaskScheduler的NoSuchBean定义
我正试图用Spring安排一项任务。这是我的代码:Spring中TaskScheduler的NoSuchBean定义,spring,scheduled-tasks,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
@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: [...]
缺少什么?在与您的
MySchedulerInvoker
相同的上下文中没有可用的TaskScheduler
bean。您需要显示一些配置,以便有人指出原因。嗯。。。我忘了在配置文件中添加任何关于调度程序的内容。