pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.13.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.13</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
spring配置文件application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<context:component-scan base-package="com" />
<context:annotation-config />
<!-- 自定义业务逻辑处理对象 -->
<bean id="TestQuartz" class="com.quartz.example2.TestQuartz"></bean>
<!-- 定义需要调用的任务对象和对象的方法 -->
<bean name="myJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 任务名 -->
<property name="name" value="one"/>
<!-- 任务组 -->
<property name="group" value="one"/>
<!-- 业务逻辑处理对象 -->
<property name="targetObject" ref="TestQuartz" />
<!-- 对象当中的方法 -->
<property name="targetMethod" value="dosomething" />
<property name="concurrent" value="false" />
</bean>
<!-- 定义触发器,设置任务触发的时间 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!-- 触发器名 -->
<property name="name" value="one"/>
<!-- 触发器组 -->
<property name="group" value="one"/>
<property name="jobDetail" ref="myJob" />
<!-- 定时任务的触发时间,现在是每秒执行一次 -->
<property name="cronExpression" value="0/1 * * * * ?" />
</bean>
<!-- 调度器 -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- 设置触发器 -->
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>
业务逻辑处理类TestQuartz
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Scheduler;
public class TestQuartz {
private Scheduler scheduler;
public void dosomething(){
Date now=new Date();
System.out.println("执行任务:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now));
}
}
测试
public static void main(String[] args) throws InterruptedException, SchedulerException {
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:application3.xml");
}