SSM项目使用@Scheduled注解fixedDelayString动态配置实现定时任务(Java)
1、在spring配置文件中加入定时任务的相关配置
1.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.xsd
"
如图:
2、xml文本
添加:
<task:annotation-driven />
如图:
3、测试类
①方法上添加@Scheduled注解
②添加@Component注解开启spring扫描
③添加@PropertySource(value = “classpath:config.properties”)注解,指定配置文件路径为:config.properties
@Component
@PropertySource(value = "classpath:config.properties")
public class Task {
@Scheduled(fixedDelayString = "${time.fixedDelay}")
public void testTask(){
System.out.println("每5秒执行一次");
}
}
如图:
4、配置文件
这里我是在config.properties文件中配置参数,如图: