1、添加命名空间和xsd约束
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
2、在 application.xml文件中开启注解
<task:annotation-driven />
3、配置扫描任务扫描包
<context:component-scan base-package="com.task" />
4、demo
@Component
public class AutoScanTask {
@Scheduled(cron = "0/5 * * * * ? ") // 间隔5秒执行
public void autoScan() {
System.out.println("定时器执行");
}
}