admin管理员组文章数量:1794759
SpringBoot+Schedule 定时任务的配置开关
启动类上添加@EnableScheduling ,开启SpringBoot自带的定时任务功能
@SpringBootApplication @EnableScheduling public class AdminApplication { public static void main(String[] args) { SpringApplication.run(AdminApplication .class, args); } }创建一个定时任务,间隔5秒执行一次
@Component public class TestScheduling { private static int i = 0; @Scheduled(fixedDelay=5*1000) public void test(){ System.out.println("this is "+ (i++) + "times!"); } }执行结果
在application.properties中添加定时任务的开关配置
## 定时任务基础配置 scheduling: enabled: true #定时任务开关修改定时任务的默认注解开关
@Component //默认条件注解是开启的,现在采用配置文件的变量来手动控制定时任务是否执行 @ConditionalOnProperty(prefix = "scheduling", name = "enabled", havingValue = "true") public class TestScheduling { private static int i = 0; @Scheduled(fixedDelay=5*1000) public void test(){ System.out.println("this is "+ (i++) + " times!"); } }
本文标签: SpringBootschedule
版权声明:本文标题:SpringBoot+Schedule 定时任务的配置开关 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686477493a71901.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论