使用 @Scheduled 和 @EnableScheduling 但给出 NoSuchBeanDefinitionException [英] Using @Scheduled and @EnableScheduling but gives NoSuchBeanDefinitionException

查看:40
本文介绍了使用 @Scheduled 和 @EnableScheduling 但给出 NoSuchBeanDefinitionException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了非常简单的例子 在线设置 Spring 中的 cron 作业,但我每次都在 Tomcat 启动日志中收到此错误:

I have followed very simple examples online to set up a cron job in Spring yet I keep getting this error in my Tomcat startup log each and every time:

2015-05-25 00:32:58 DEBUG ScheduledAnnotationBeanPostProcessor:191 - 
Could not find default TaskScheduler bean org.springframework.beans.factory.NoSuchBeanDefinitionException: No 
qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined

2015-05-25 00:32:58 DEBUG ScheduledAnnotationBeanPostProcessor:202 - Could not    
find default ScheduledExecutorService bean
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying    
bean of type [org.springframework.scheduling.TaskScheduler] is defined

以及用于实现 cron 的 2 个 java 类:

And the 2 java classes used to implement the cron:

  • @Configuration 类:

  • The @Configuration class:

@Configuration
@EnableScheduling
public class ClearTokenStoreCronEnable {    
  final static Logger log =   
  LoggerFactory.getLogger(ClearTokenStoreCronEnable.class);
  private @Autowired TokenStoreRepository tokenStoreRepository; 
}

  • 和 Cron 作业类:

  • and Cron job class:

    @Service
    public class ClearTokenStoreWorkerService {
    
        final static Logger log = LoggerFactory.getLogger(ClearTokenStoreWorkerService.class);
        private @Autowired TokenStoreRepository tokenStoreRepository;
    
        //@Scheduled(fixedDelay=5000)
        //run daily at midnight
        @Scheduled(cron = "0 0 * * * *")
        public void tokenStoreTable() {
            log.debug("tokenstore table truncated - start");
            tokenStoreRepository.deleteAll();
            log.debug("tokenstore table truncated - end");
        }
    }
    

  • 顺便说一下,cron 作业在午夜运行,但它似乎也在其他时间随机运行.不确定这是错误还是我的 cron 表达式错误:@Scheduled(cron = "0 0 * * * *")

    As a side note, the cron job runs at midnight but it also seems to run randomly at other times. Not sure if this is a bug or my cron expression is wrong: @Scheduled(cron = "0 0 * * * *")

    我此时主要关心的是为什么我会收到 ScheduledAnnotationBeanPostProcessor 错误?它正在寻找 TaskScheduler 和 ScheduledExectorService. 我只需要每天触发一次.我没有做任何并发处理或我需要多个线程的地方.这些错误最终是有害的还是我需要修复它们?

    My main concern at this time is why am I getting ScheduledAnnotationBeanPostProcessor errors? It's looking for a TaskScheduler and ScheduledExectorService. I just need to fire this once a day. I am not doing any concurrent processing or where I need multiple threads. Ultimately are these errors harmful OR do I need to fix them?

    推荐答案

    根据异常信息 Could not find default TaskScheduler bean,配置应该定义 TaskScheduler 而不是 "执行者

    according to exception Info Could not find default TaskScheduler bean, the config should define TaskScheduler rather than "Executor"

    @Configuration
    public class AppContext extends WebMvcConfigurationSupport {
        @Bean
        public TaskScheduler taskScheduler() {
            return new ConcurrentTaskScheduler();
        }
    
        // Of course , you can define the Executor too
        @Bean
        public Executor taskExecutor() {
            return new SimpleAsyncTaskExecutor();
       }
    }
    

    这篇关于使用 @Scheduled 和 @EnableScheduling 但给出 NoSuchBeanDefinitionException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆