使用spring @Scheduled注解执行定时任务

时间:2022-03-08 07:51:54

spring.xml 的 xmlns 添加:

xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation添加:

http://www.springframework.org/schema/task  

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

添加定时任务注解

<task:annotation-driven/> 

添加注解扫描的包

 <context:component-scan base-package="com.demo.service"/>  

java文件

package com.demo.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class DemoService {
@Scheduled(cron="0/5 * * * * ? ")
public void test(){
System.out.println("test is run.");
}
}
至于cron 百度有很多