1. application.xml:
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd
在<beans>外面配置如下:
<!--开启Spring Task定时任务的注解模式-->
<task:annotation-driven/>
package com.imooc.reader.task;
/*
* description: 完成自动计算任务 [描述信息]
* @author wu-weixin [作者]
* @date 2022/06/25 02:19:44 [时间,这里是年/月/日 时:分:秒的格式]
* @version 1.0.0 [版本信息]
*
**/
import com.imooc.reader.service.BookService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
/*
* description 完成自动计算任务
* @author wu-weixin
* @date 2022/6/25 4:09
* @version 1.0.0
**/
@Component //无法确定是service,还是其它,所以使用component
public class ComputeTask {
@Resource
private BookService bookService;
@Scheduled(cron = "0 * * * * ?")
public void updateEvaluation(){
bookService.updateEvalution();
System.out.println("已更新所有图书评分: "+new Date());
}
}