2. spring定时任务

时间:2021-08-03 14:52:21

1.添加命名空间

2. spring定时任务2. spring定时任务

    <!-- 定时注解驱动 -->
<task:annotation-driven />
<!-- 进行定时任务的类,将其定义为一个bean -->
<bean id="testJob" class="com.test.TestJob"></bean>
<!-- 通过task标签,定义定时功能,每5秒执行一次 -->
<task:scheduled-tasks>
<task:scheduled ref="testJob" method="excute" cron="0/5 * * * * ?" />
</task:scheduled-tasks>

定时任务类如下:

package com.test;
import org.springframework.stereotype.Component; @Component
public class TestJob{ public void excute() {
System.out.println("开始执行定时任务!");
} }