引入依赖

1
2
3
4
5
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.ledao.quartz;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.*;

/**
* 每天23点自动删除已被删除文章中的图片
*
* @author LeDao
* @company
* @create 2020-09-23 11:27
*/
@Configuration
@EnableScheduling
public class DeleteInvalidImage {

@Resource
private BlogService blogService;

@Scheduled(cron = "0 0 23 * * ?")
public void work() {
//这里填入定时执行的代码
}
}

PS.

常用cron表达式:常用 cron 表达式 | LeDao’s Blog (zoutl.cn)