Only no-arg methods may be annotated with @Scheduled 的解决方法

问题描述

今天在写爬虫代码时,需要个定时抓取任务,测试时SpringBoot项目启动时报错,记录一下问题

Only no-arg methods may be annotated with @Scheduled

控制台输出错误日志:

Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'commentSubmit': Only no-arg methods may be annotated with @Scheduled
	at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:518)
	at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$null$1(ScheduledAnnotationBeanPostProcessor.java:380)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$postProcessAfterInitialization$2(ScheduledAnnotationBeanPostProcessor.java:380)
	at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684)
	at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:379)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:455)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1808)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620)
	... 15 more

在这里插入图片描述

原因分析:

报错的大致意思是使用@Scheduled注解的方法不能带参数,看了下自己的代码,确实存在这个问题,那就很好解决了

解决方案:

1、可以选择不适用SpringBoot自带Scheduler,使用xml配置等其他方式实现定时任务

2、如果确定使用Scheduler,那只有根据自己的业务需求改造方法了

posted on 2022-07-12 21:59  猫的树kireCat  阅读(432)  评论(0编辑  收藏  举报