定时任务
项目启动 , 定时执行
spring.xml中的配置
xml中配置
<!-- 定时任务 -->
<task:scheduled-tasks>
<task:scheduled ref="job1" method="run" cron="10/5 * * * * ?"/>
</task:scheduled-tasks>
<!-- 包的扫描 -->
<context:component-scan base-package="com.wanmait.hospital.job"/>
自动注入,注解实现
<!-- 定时任务 -->
<!--<task:scheduled-tasks>
<task:scheduled ref="job1" method="run" cron="10/5 * * * * ?"/>
</task:scheduled-tasks>-->
<task:annotation-driven/>
<context:component-scan base-package="com.wanmait.mqa.job"/>
测试类
对应xml配置
package com.wanmait.mqa.job;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class Job1 {
public void run()
{
System.out.println("job1.........."+new Date());
}
}
对应注解配置
package com.wanmait.mqa.job;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class Job2 {
@Scheduled(cron = "0/3 * * ? * *")
public void run()
{
System.out.println("job2................"+new Date());
}
@Scheduled(cron = "0 0 2 * * ?")
public void clearCache()
{
}
}