Spring Boot:
@EnableScheduling turn on scheduled task support,
@Scheduled Scheduled Task statement
1 Package Ch2.scheduler2;2 3 //Date Conversion Mode4 import Java.text.SimpleDateFormat;5 import java.util.Date;6 7 //Scheduled Task Statement8 import org.springframework.scheduling.annotation.Scheduled;9 //Spring Component AnnotationsTen import Org.springframework.stereotype.Service; One A @Service - Public classSchedulerservice { - the Private StaticFinal SimpleDateFormat DateFormat =NewSimpleDateFormat ("Hh::mm::ss"); - -@Scheduled (fixedrate= the) - Public voidprofixcurrenttime () + { -System. out. println ("executes every 5 seconds:"+ Dateformat.format (NewDate ())); + } A at@Scheduled (cron="0? * *") - Public voidcorncurrenttime () - { -System. out. println ("Custom Execution Time:"+ Dateformat.format (NewDate ())); - } - in -}
1 Package Ch2.scheduler2;2 3 //Introducing Spring Configuration annotations4 import org.springframework.context.annotation.Configuration;5 //introducing Spring Auto-load annotations6 import Org.springframework.context.annotation.ComponentScan;7 8 //Scheduled Task declaration class: Open a Scheduled task statement9 import org.springframework.scheduling.annotation.EnableScheduling;Ten One //spring Configuration class declaration A @Configuration - //Automatic introduction of service,component under current package .... -@ComponentScan ("Ch2.scheduler2") the //turn on support for scheduled tasks - @EnableScheduling - Public classTaskschedulerconfig { - +}
1 Package Ch2.scheduler2;2 //Introducing Containers3 import Org.springframework.context.annotation.AnnotationConfigApplicationContext;4 5 Public classMain {6 7 Public Static voidMain (string[] args)8 {9 TenAnnotationconfigapplicationcontext context =NewAnnotationconfigapplicationcontext (Taskschedulerconfig.class); One //Schedulerservice schedulerservice = Context.getbean (schedulerservice.class); A - } - the}
Spring boot: @EnableScheduling enable scheduled task support, @Scheduled scheduled task statement