1.在启动类上面配置注解
@EnableScheduling
@EnableAsync
@SpringBootApplication
public class DdingApplication {
public static void main(String[] args) {
SpringApplication.run(DdingApplication.class, args);
}
}
2在目标工具类上面配置注解以及代码
@Component
public class Test {
// 清除缓存的会员数据
@Async
@Scheduled(cron = "0/5 * * * * *")
public void Time() {
System.out.println(111);
}
3.启动Springboot项目
2021-05-24 09:57:03.411 INFO 12968 --- [ main] com.example.dding.DdingApplication : Starting DdingApplication using Java 1.8.0_191 on LAPTOP-OL00SML3 with PID 12968 (G:\20210327\ceshiDaima\target\classes started by 12964 in G:\20210327\ceshiDaima)
2021-05-24 09:57:03.413 INFO 12968 --- [ main] com.example.dding.DdingApplication : No active profile set, falling back to default profiles: default
2021-05-24 09:57:04.229 INFO 12968 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-05-24 09:57:04.236 INFO 12968 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-05-24 09:57:04.236 INFO 12968 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.46]
2021-05-24 09:57:04.317 INFO 12968 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-05-24 09:57:04.317 INFO 12968 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 842 ms
2021-05-24 09:57:05.106 INFO 12968 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-05-24 09:57:05.117 INFO 12968 --- [ main] com.example.dding.DdingApplication : Started DdingApplication in 2.051 seconds (JVM running for 2.986)
2021-05-24 09:57:05.119 INFO 12968 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state LivenessState changed to CORRECT
2021-05-24 09:57:05.120 INFO 12968 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
111
111
111
111
111
111
111
111
111
111
111