Quartz-Spring集成Quartz通过注解配置的方式
2017-11-12 20:31
387 查看
概述
步骤
配置文件中增加task命名空间
配置Spring扫描和task扫描
编写带有注解的Job类
示例
Scheduled解读
示例源码
Spring配置文件
带有注解的Job类
测试类
运行结果
可配置属性说明
步骤
配置文件中增加task命名空间
配置Spring扫描和task扫描
编写带有注解的Job类
示例
Scheduled解读
示例源码
概述
上一篇博文Quartz-Spring集成Quartz通过XML配置的方式中我们了解到了通过xml配置的方式集成Quartz,我们发现使用xml的方式,会配置很多bean的信息,但是如果使用注解的方式,会更方便,配置注解相对简单。步骤
配置文件中增加task命名空间
xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
配置Spring扫描和task扫描
<!-- 定时任务扫描 --> <task:annotation-driven/>
编写带有注解的Job类
详见示例部分示例
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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <!-- 扫描组件 --> <context:component-scan base-package="com.xgj.quartz.quartzWithSpring.anno"/> <!-- 定时任务扫描 --> <task:annotation-driven/> </beans>
带有注解的Job类
package com.xgj.quartz.quartzWithSpring.anno; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class MyAnnoJob { @Scheduled(cron = "*/5 * * * * ?") // 每隔5秒执行一次 public void test() throws Exception { System.out.println("Spring集成Quartz 使用 Annotation的方式......"); } }
测试类
package com.xgj.quartz.quartzWithSpring.anno; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringQuartzAnnoTest { public static void main(String[] args) { // 启动Spring 容器 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( "classpath:com/xgj/quartz/quartzWithSpring/anno/spring-quartz-anno.xml"); System.out.println("initContext successfully"); } }
运行结果
2017-11-12 08:44:44,353 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6fbdd27a: startup date [Sun Nov 12 08:44:44 BOT 2017]; root of context hierarchy 2017-11-12 08:44:44,436 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/quartz/quartzWithSpring/anno/spring-quartz-anno.xml] 2017-11-12 08:44:45,435 INFO [main] (ScheduledAnnotationBeanPostProcessor.java:262) - No TaskScheduler/ScheduledExecutorService bean found for scheduled processing initContext successfully Spring集成Quartz 使用 Annotation的方式...... Spring集成Quartz 使用 Annotation的方式...... Spring集成Quartz 使用 Annotation的方式...... Spring集成Quartz 使用 Annotation的方式...... Spring集成Quartz 使用 Annotation的方式...... ...... ...... ...... ......省略....
@Scheduled解读
我们来看下源码package org.springframework.scheduling.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Repeatable(Schedules.class) public @interface Scheduled { String cron() default ""; String zone() default ""; long fixedDelay() default -1L; String fixedDelayString() default ""; long fixedRate() default -1L; String fixedRateString() default ""; long initialDelay() default -1L; String initialDelayString() default ""; }
可配置属性说明
属性类型 | 属性 | 属性说明 |
---|---|---|
String | cron | cron的表达式 |
String | zone | cron表达式将被解析的时区 |
long | fixedDelay | 在最后一次调用结束和下一次调用开始之间的固定时间段执行注释方法。 |
String | fixedDelayString | 在最后一次调用结束和下一次调用开始之间的固定时间段执行注释方法。 |
long | fixedRate | 在调用之间以固定的时间段执行带注释的方法。 |
String | fixedRateString | 在调用之间以固定的时间段执行带注释的方法。 |
long | initialDelay | 在首次执行fixedRate()或fixedDelay()任务之前要延迟的毫秒数。 |
String | initialDelayString | 在首次执行fixedRate()或fixedDelay()任务之前要延迟的毫秒数。 |
示例源码
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster相关文章推荐
- Quartz-Spring集成Quartz通过XML配置的方式
- 原始方式:spring四种依赖注入方式 |注解方式:Spring零配置通过注解实现Bean依赖注入
- spring boot 集成quartz 2.0 实现前端动态配置(获取spring上下文)的两种方式,启动数据库中已开启定时任务
- spring笔记11---通过注解的方式配置bean
- spring boot框架学习学前掌握之重要注解(2)-通过java的配置方式进行配置spring
- 在Spring3 MVC中五步配置集成注解方式Hibernate3
- Spring通过java注解的方式进行配置annotation
- spring boot 集成quartz 2.0 实现前端动态配置(获取spring上下文)的两种方式,启动数据库中已开启定时任务
- Spring-12:通过注解的方式配置Bean
- spring boot框架学习学前掌握之重要注解(2)-通过java的配置方式进行配置spring
- spring boot框架学习学前掌握之重要注解(2)-通过java的配置方式进行配置spring
- 【Quartz】基于Spring注解方式配置Quartz
- 通过注解方式配置Spring实现Ioc
- 通过注解方式配置Spring的IoC
- 通过注解的方式集成Spring 4 MVC+Hibernate 4+MySQL+Maven,开发项目样例
- 【Quartz】基于Spring注解方式配置Quartz
- spring通过注解依赖注入和获取xml配置混合的方式
- 基于Spring注解方式配置Quartz
- 在Spring3 MVC中五步配置集成注解方式Hibernate3
- Spring使用Aspectj注解的方式来配置AOP