spring.xml 的 xmlns 添加:
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.1.xsd
添加定时任务注解
<task:annotation-driven/> 添加注解扫描的包
<context:component-scan base-package="com.demo.service"/> java文件
package com.demo.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class DemoService {
@Scheduled(cron="0/5 * * * * ? ")
public void test(){
System.out.println("test is run.");
}
}
至于cron 百度有很多
本文介绍了如何在Spring框架中配置并实现定时任务。通过在spring.xml中添加必要的xmlns和schemaLocation属性,启用注解驱动的定时任务,并指定组件扫描的基础包路径。示例展示了如何使用@Component和@Scheduled注解创建一个每5秒执行一次的任务。
940

被折叠的 条评论
为什么被折叠?



