spring 配置定时任务Scheduled


一:在spring配置的xml文件添加3条命名空间

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.0.xsd 
  "

二:在spring配置的xml文件中添加

   <!-- 开启定时任务 -->
    <task:annotation-driven />
    <!-- 开启注解 -->
    <context:annotation-config />

三:在某个定时任务类上添加被扫描的注解@Component,以及在定时执行的方法上添加@Scheduled(cron表达式)

@Component
public class ScheduledTest  {

    @Scheduled(cron="*/3 * * * * ?") //每3秒执行1次
    public void start() {
        System.out.println("每3秒执行一次");
    }

}

四:启动应用,则会在控制台输出


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM