我正在使用spring-boot
来设置弹出默认值。
我想使用@EnableScheduling
机制,并按条件安排我的任务。
因此,我必须实施SchedulingConfigurer
并明确设置TaskScheduler
。
但是在注入TaskScheduler
时,我收到以下错误。但是为什么spring-boot不会相应地自动提供调度程序呢?
@Configuration
@EnableAutoConfiguration
@EnableScheduling
public class AppConfig {
}
@Service
public class JobService implements SchedulingConfigurer {
@Autowired
private TaskScheduler taskScheduler;
//schedule the task dynamically
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskScheduler); //fails
taskRegistrar.addTriggerTask(task, trigger);
}
}
错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.scheduling.TaskScheduler; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying 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: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 15 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying 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: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 17 more
答案 0 :(得分:8)
您无需在ScheduledTaskRegistrar
上设置调度程序。如果尚未配置,则默认为包含单线程预定执行程序的ConcurrentTaskScheduler
:
if (this.taskScheduler == null) {
this.localExecutor = Executors.newSingleThreadScheduledExecutor();
this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor);
}
如果您对此默认计划程序感到满意,可以删除TaskScheduler
的自动装配以及将其设置在ScheduledTaskRegistrar
上的通话。另外,正如Marten在评论中建议的那样,您应该使SchedulingConfigurer
成为配置类而不是服务。
这些更改会使您的代码看起来像这样:
@Configuration
static class TaskConfiguration implements SchedulingConfigurer {
//schedule the task dynamically
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(task, trigger);
}
}