1. 程式人生 > >基於註解使用定時框架Quartz

基於註解使用定時框架Quartz

最近複習一下Spring整合Quartz.跑了一個demo特此記錄

package task;

import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Create by szw on 2017/11/23 10:05
 */
@Component
@Lazy(false)
public class SpringTask {
    @Scheduled(cron="0/1 * *  * * ? ")
    public void task() {
        System.out.println("我執行了");
    }
}

需要在spring的配置檔案開啟任務註解(別忘了要配置spring掃描相關包)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
    <!--<context:component-scan base-package="controller"/>-->
    <context:component-scan base-package="task"/>
    <task:annotation-driven/>
</beans>