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