1

1.spring配置文件配置

注册自定义作业类

    <bean id="myJob" class="me.zhenggg.jobs.MyJob">
        <property name="string" value="I am quantz job"/>
    </bean>

配置JobDetail

    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 注入目标对象 -->
        <property name="targetObject" ref="myJob"/>
        <!-- 注入目标方法 -->
        <property name="targetMethod" value="execute"/>
    </bean>

配置触发器

    <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- 注入任务详情对象 -->
        <property name="jobDetail" ref="jobDetail"/>
        <!-- 注入cron表达式,通过这个表达式指定触发的时间点 -->
        <property name="cronExpression">
            <value>0/5 * * * * ?</value>
        </property>
    </bean>

配置调度工厂

    <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <!-- 注入触发器 -->
        <property name="triggers">
            <list>
                <ref bean="myTrigger"/>
            </list>
        </property>
    </bean>

2.编写MyJob类的定时任务业务逻辑

package me.zhenggg.jobs;

import java.util.Date;
/**
 * @author zhenggg on 2017/5/29.
 */
public class MyJob {

    private String string;
    public String getString() {
        return string;
    }
    public void setString(String string) {
        this.string = string;
    }
    /**
     * 定时任务具体的业务逻辑
     */
    public void execute() {
         System.out.println(this.getString()+new Date());
    }
}

3.结果

结果


铮gg
93 声望12 粉丝