# 使用注解:定义一个组件,扫描一下包即可
| package com.spring.schedule.demo01; |
| |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| import org.springframework.scheduling.annotation.EnableScheduling; |
| import org.springframework.scheduling.annotation.Scheduled; |
| import org.springframework.stereotype.Component; |
| |
| import java.text.SimpleDateFormat; |
| import java.util.Date; |
| |
| @Component |
| @EnableScheduling |
| public class ScheduledTasksForAnnotation { |
| |
| private static final Logger log = LoggerFactory.getLogger(ScheduledTasksForAnnotation.class); |
| |
| private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); |
| |
| |
| @Scheduled(fixedRate = 5000) |
| |
| |
| public void reportCurrentTime() { |
| System.out.println("日志打印时间:"+dateFormat.format(new Date())); |
| } |
| |
| } |
| |
| |
| <context:component-scan base-package="com.spring.schedule"> |
| <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> |
| </context:component-scan> |
如果不想使用 @EnableScheduling, 则需增加如下配置
# 使用 quartz
增加一个 spring-jobs.xml 配置,引入到 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:cache="http://www.springframework.org/schema/cache" |
| xsi:schemaLocation=" |
| http://www.springframework.org/schema/beans |
| http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
| http://www.springframework.org/schema/context |
| http://www.springframework.org/schema/context/spring-context-3.0.xsd |
| http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd" > |
| |
| |
| <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> |
| <property name="triggers"> |
| <list> |
| |
| <ref local="myTaskTrigger"/> |
| |
| </list> |
| </property> |
| </bean> |
| |
| |
| |
| <bean id="myTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> |
| <property name="jobDetail"> |
| <ref bean="myTask" /> |
| </property> |
| <property name="cronExpression"> |
| |
| |
| |
| |
| <value>*/5 * * * * ?</value> |
| </property> |
| </bean> |
| |
| |
| <bean id="myTask" |
| class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> |
| <property name="targetObject"> |
| <bean class="com.spring.schedule.demo02.ScheduledTaskForQuartz" /> |
| </property> |
| <property name="targetMethod"> |
| <value>reportCurrentTime</value> |
| </property> |
| </bean> |
| |
| </beans> |
下面是实际任务的类
| package com.spring.schedule.demo02; |
| |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| import org.springframework.stereotype.Component; |
| |
| import java.text.SimpleDateFormat; |
| import java.util.Date; |
| |
| @Component |
| public class ScheduledTaskForQuartz { |
| |
| private static final Logger log = LoggerFactory.getLogger(ScheduledTaskForQuartz.class); |
| |
| private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); |
| |
| public void reportCurrentTime() { |
| System.out.println("日志打印时间:"+dateFormat.format(new Date())); |
| } |
| |
| } |
代码可以参考我的 github:git@github.com:SincerelyUnique/spring-task.git