0/5 * * * * ? ... ...
<!-- 註冊自定義任務 --> <bean id="myJob" class="cn.itheima.quartz.MyJob"></bean> <!-- jobDetail --> <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="myJob"></property> <property name="targetMethod" value="sendMsg"></property> </bean> <!-- 觸發器 --> <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!-- 註入jobDetail --> <property name="jobDetail" ref="myJobDetail"> </property> <!-- 通過表達式指定配置任務觸發時間 --> <property name="cronExpression"> <!-- 每5秒鐘執行一次任務 --> <value>0/5 * * * * ?</value> </property> </bean> <!-- 配置調度工廠對象 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="myTrigger"/> </list> </property> </bean>
---------------------------------------------------------------------
其中MyJob相當於Service層:
import java.util.Date; public class MyJob { public void sendMsg(){ System.out.println("自定義的任務被執行了,執行時間為:"+new Date()); } }