问题描述
今天在写爬虫代码时,需要个定时抓取任务,测试时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,那只有根据自己的业务需求改造方法了
创作不易,关注💖、点赞👍、收藏🎉就是对作者最大的鼓励👏,欢迎在下方评论留言🧐