SpringBoot @Scheduled 使用

2020-07-03  本文已影响0人  Jong_q

@Scheduled 是 Spring 框架中的注解

在Spring 框架下的使用方式是

1、xmlns 中需要添加

http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd
xmlns:task="http://www.springframework.org/schema/task"

2、注入

<task:annotation-driven/>

3、代码中

@Component
public class Task2Service {
    @Scheduled(cron = "0/5 * * * * ? *")
    public void task () {
        System.out.println("Scheduled 任务测试");
    }
}

SpringBoot 的注解自动装配

1、在启动类上 添加注解

@SpringBootApplication(scanBasePackages = "test")
@RestController
@EnableScheduling
public class SpringDataApplication {

}

2、代码中

@Component
public class Task2Service {
    @Scheduled(cron = "0/5 * * * * ? *")
    public void task () {
        System.out.println("Scheduled 任务测试");
    }
}

最后,结果输出


image.png
上一篇 下一篇

猜你喜欢

热点阅读