实例一:

1、引包

<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
		</dependency>
		<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz-jobs</artifactId>
		</dependency>

 2、编写配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
       default-autowire="byName">

    <task:scheduler id="jobScheduler" pool-size="4"/>

    <bean name="myjobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <!-- 指定具体的job类 -->
        <property name="jobClass" value="com.fire.demo.job.MyJob" />
        <!-- 指定job的名称 -->
        <property name="name" value="myJob" />
        <!-- 指定job的分组 -->
        <property name="group" value="jobs" />
        <!-- 必须设置为true,如果为false,当没有活动的触发器与之关联时会在调度器中删除该任务  -->
        <property name="durability" value="true"/>
        <!-- 指定spring容器的key,如果不设定在job中的jobmap中是获取不到spring容器的 -->
        <property name="applicationContextJobDataKey" value="adc"/>
        <property name="jobDataMap">
            <map>
                <entry key="water" value="水"/>
                <entry key="pen" value="笔"/>
            </map>
        </property>
    </bean>

    <!-- 定义触发器 -->
    <bean id="cronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="myjobDetail" />
        <!-- 每一分钟执行一次 -->
        <property name="cronExpression" value="*/5 * * * * ?" />
    </bean>

    <!-- 定义调度器 -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTrigger1" />
            </list>
        </property>
    </bean>

</beans>

3、编写任务

package com.fire.demo.job;

import com.fire.demo.service.UserService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;

public class MyJob extends QuartzJobBean {
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {

        System.out.println("Myjob 执行了..........." + context.getTrigger().getKey().getName());
        ApplicationContext applicationContext = (ApplicationContext) context
                .getJobDetail().getJobDataMap().get("adc");
        UserService userService = applicationContext.getBean(UserService.class);
        String user = userService.getUser();
        System.out.println("user=" + user);
        System.out.println("当前时间:" + new Date().toString());

    }
}

4、启动类导入配置

@ImportResource(value = "classpath:/spring/spring_quartz.xml")

 方案二:

1、编写配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
       default-autowire="byName">

    <task:scheduler id="jobScheduler" pool-size="4"/>

    <bean id="penJob" class="com.fire.demo.job.PenJob" />
    <task:scheduled-tasks scheduler="jobScheduler">
        <task:scheduled ref="penJob" method="excute" cron="*/5 * * * * ?"/>
    </task:scheduled-tasks>
</beans>

2、编写job

package com.fire.demo.job;

import com.fire.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
public class PenJob {
    @Autowired
    private UserService userService;

    public void excute() {
        log.info("pen----" + userService.getUser());
    }
}

3、启动类导入配置文件

@ImportResource(value = "classpath:/spring/spring_quartz.xml")