spring,springmvc 定时任务

本文介绍了Spring Boot中使用注解和非注解两种方式实现定时任务的方法,包括配置文件、类定义及具体实现步骤。

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

注解模式



加上该标头
<beans  xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation=" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">



配置文件
   
<!-- 配置注解定时扫描任务   -->  
     <task:annotation-driven/>  
   <!-- 扫描路径  -->   
    <context:component-scan base-package="扫描包的地址">
   </context:component-scan> 


    
CLASS代码
// 表示扫描该定时任务
@Component
public class Class{ 
	// 时间间隔,10秒运行一次
	@Scheduled(cron = "0/10 * * * * ? ")
	public void taskCycle() {
		//code
	}
}






非注解模式



加上该标头
<beans  xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation=" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">



配置文件
<!-- ref参数指定的即任务类,method指定的即需要运行的方法,cron表示多长时间运行一次(60秒运行一次) -->
<task:scheduled-tasks>
<task:scheduled ref="任务类" method="运行的方法" cron="0/60 * * * * ?"/>
</task:scheduled-tasks>  
<context:component-scan base-package="扫描的包" />  




CLASS
@Service("注入service,必须注入")
public class SalseandloadLogController { 
	public void IntegralRuleFlexible() { 
		//code
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值