Spring Boot 中使用 @Scheduled 注解创建定时任务

本文介绍了如何在Spring Boot中利用@Scheduled注解创建定时任务,以实现如清理15天前数据等自动操作。首先,在pom.xml中添加Spring Boot依赖,接着在启动类上启用定时任务功能,最后编写定时任务方法,支持cron表达式进行更灵活的定时配置。
摘要由CSDN通过智能技术生成

比如数据库中有些表中的数据要求保存15天,那15天之前的数据就会清除,可以在每天的比如12时调用方法清除15天之前的数据,这时就可以用定时任务来完成。

Spring Boot 创建定时任务

Spring Boot 默认已经实现了定时任务,只需要添加相应的注解即可完成

1.

pom.xml 文件配置

pom.xml 不需要添加其他依赖,只需要加入 Spring Boot 依赖即可,这里我们添加一个 web 和 test 的依赖


<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
	</depende
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值