SpringBoot2.1 - Quartz自动化配置集成 yml方式配置免配置类和properties

本文介绍了如何在SpringBoot 2.1中利用新特性实现Quartz的自动化配置,通过YML方式进行配置,不再需要手动配置类。内容包括删除原有配置类,理解自动化配置的源码实现,添加YML配置以启用数据库存储任务,并提供测试验证自动化配置已成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先非常感谢原作者恒宇少年 ,写了一遍好文章。找了很多文章终于找到比较有用有价值的高质量博文。

原贴地址:https://www.jianshu.com/p/056281e057b3

 

以下引用原文章。

在新版本的SpringBoot2.0发布后,急迫尝鲜的我将相关的项目已经更换为最新版本,在SpringBoot源码GitHub看到更新日志,表明了针对Quartz新版本进行了 AutoConfiguration自动化配置,省去了很多繁琐的配置。

官网更新日志

Auto-configuration support is now include for the Quartz Scheduler. We’ve also added a new spring-boot-starter-quartz starter POM.
You can use in-memory JobStores, or a full JDBC-based store. All JobDetail, Calendar and Trigger beans from your Spring application context will be automatically registered with the Scheduler.
For more details read the new "Quartz Scheduler" section of the reference documentation.

SpringBoot2.0版本集成了Quartz2.3.0官网最新版本。

本章目标

使用SpringBoot2.0新特性完成Quartz自动化配置。

SpringBoot 企业级核心技术学习专题


专题 专题名称 专题描述
001 Spring Boot 核心技术 讲解SpringBoot一些企业级层面的核心组件
002 Spring Boot 核心技术章节源码 Spring Boot 核心技术简书每一篇文章码云对应源码
003 Spring Cloud 核心技术 对Spring Cloud核心技术全面讲解
004 Spring Cloud 核心技术章节源码 Spring Cloud 核心技术简书每一篇文章对应源码
005 QueryDSL 核心技术 全面讲解QueryDSL核心技术以及基于SpringBoot整合SpringDataJPA
006 SpringDataJPA 核心技术 全面讲解SpringDataJPA核心技术
007 SpringBoot核心技术学习目录 SpringBoot系统的学习目录,敬请关注点赞!!!

构建项目

在前面章节第四十章:基于SpringBoot & Quartz完成定时任务分布式多节点负载持久化内我们已经通过添加配置的方式完成集成,为了本章的方便直接复制之前的项目,在基础上进行修改。
打开pom.xml配置文件,SpringBoot为我们提供了对应的依赖,我们将之前的quartz相关依赖删除,替换为spring-boot-starter-quartz,如下所示:

<!--quartz相关依赖-->
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>${quartz.version}</version>
</dependency>
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz-jobs</artifactId>
    <version>${quartz.version}</version>
</dependency>
>>>>替换为:>>>>
<!--quartz依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
......

删除QuartzConfiguration配置类

在之前章节我们使用QuartzConfiguration配置类来完成了Quartz需要的一系列配置,如:JobFactorySchedulerFactoryBean等,在我们添加spring-boot-starter-quartz依赖后就不需要主动声明工厂类,因为spring-boot-starter-quartz已经为我们自动化配置好了。

自动化配置源码

我们找到IdeaExternal Libraries并且展开spring-boot-autoconfigure-2.0.0.RELEASE.jar,找到org.springframework.boot.autoconfigure.quartz,该目录就是SpringBoot为我们提供的Quartz自动化配置源码实现,在该目录下有如下所示几个类:

  • AutowireCapableBeanJobFactory
    该类替代了我们之前在QuartzConfiguration配置类的AutowiringSpringBeanJobFactory内部类实现,主要作用是我们自定义的QuartzJobBean子类被Spring IOC进行托管,可以在定时任务类内使用注入任意被Spring IOC托管的类。
  • JobStoreType
    该类是一个枚举类型,定义了对应application.ymlapplication.properties文件内spring.quartz.job-store-type配置,其目的是配置quartz任务的数据存储方式,分别为:MEMORY(内存方式:默认)、JDBC(数据库方式)。
  • QuartzAutoConfiguration
    该类是自动配置的主类,内部配置了SchedulerFactoryBean以及JdbcStoreTypeConfiguration,使用QuartzProperties作为属性自动化配置条件。
  • QuartzDataSourceInitializer
    该类主要用于数据源初始化后的一些操作,根据不同平台类型的数据库进行选择不同的数据库脚本。
  • QuartzProperties
    该类对应了spring.quartzapplication.ymlapplication.properties文件内开头的相关配置。
  • SchedulerFactoryBeanCustomizer
    这是一个接口,我们实现该接口后并且将实现类使用Spring IOC托管,可以完成SchedulerFactoryBean的个性化设置,这里的设置完全可以对SchedulerFactoryBean做出全部的设置变更。

spring.quartz配置

看到QuartzAutoConfiguration类源码,我们知道了,想要使用自动化配置,需要满足QuartzProperties属性配置类的初始化,所以我们需要再application.ymlapplication.properties配置文件内添加对应的配置信息,如下所示:

spring:
  quartz:
    #相关属性配置
    properties:
      org:
        quartz:
          scheduler:
            instanceName: clusteredScheduler
            instanceId: AUTO
          jobStore:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
            tablePrefix: QRTZ_
            isClustered: true
            clusterCheckinInterval: 10000
            useProperties: false
          threadPool:
            class: org.quartz.simpl.SimpleThreadPool
            threadCount: 10
            threadPriority: 5
            threadsInheritContextClassLoaderOfInitializingThread: true
    #数据库方式
    job-store-type: jdbc
    #初始化表结构
    #jdbc:
      #initialize-schema: never
  • spring.quartz.properties
    该配置其实代替了之前的quartz.properties,我们把之前quartz.properties配置文件内的所有配置转换成YUML风格,对应的添加在该配置下即可,在QuartzAutoConfiguration类内,会自动调用SchedulerFactoryBeansetQuartzProperties方法,把sp
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值