个人笔记(自动更新task)

本文介绍了如何在Spring Boot应用中使用`@Scheduled`注解创建定时任务,重点讲解了在`ComputeTask`组件中通过`BookService`自动更新图书评分的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值