Spring uses annotations to execute scheduled tasks
1. Add the following configuration in SpringContext. xml
1. Add xmlns to beans: task
xmlns:task="http://www.springframework.org/schema/task"
2. Add in xsi: schemaLocation
http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.0.xsd
3. Add bean and task labels
4. Add a scan package
2. Java code for scheduled tasks
Package com. task. springTask; import org. springframework. scheduling. annotation. scheduled; import org. springframework. stereotype. component; @ Component ("springTask") public class SpringTask {@ Scheduled (cron = "0/2 ****? ") Public void myTask () {System. out. println (" this task is executed once every two seconds! ");}}
Execution result
Description <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3Ryb25nPqO6PC9wPgo8cD4xLiBAU2NoZWR1bGVkKGNyb24gPSA = "0/2 ****? ") The cron in is used to set the time of the scheduled task. The specific setting method is Baidu Google.