iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Spring中的@Scheduled功能怎么使用
  • 571
分享到

Spring中的@Scheduled功能怎么使用

2023-06-29 22:06:13 571人浏览 泡泡鱼
摘要

这篇文章主要介绍“spring中的@Scheduled功能怎么使用”,在日常操作中,相信很多人在Spring中的@Scheduled功能怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Spring中的@

这篇文章主要介绍“spring中的@Scheduled功能怎么使用”,在日常操作中,相信很多人在Spring中的@Scheduled功能怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Spring中的@Scheduled功能怎么使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

    一、Spring @Scheduled Annotation

    @ scheduled注释用于任务调度。触发器信息需要与这个注释一起提供。

    您可以使用属性 fixedDelay/fixedRate/cron 来提供触发信息。

    • fixedRate 使 Spring 定期运行任务,即使最后一次调用仍在运行

    • fixedDelay 特别控制最后一次执行结束时的下一次执行时间。

    • Cron 是一个源自 Unix cron 实用工具的特性,并且根据您的需求有各种选项。

    示例用法如下:

    @Scheduled Usages@Scheduled(fixedDelay =30000)public void demoServiceMethod () {... } @Scheduled(fixedRate=30000)public void demoServiceMethod () {... } @Scheduled(cron="0 0 * * * *")public void demoServiceMethod () {... }

    1.2 如何启用@Scheduled 注释

    要在 spring 应用程序中使用@Scheduled,必须首先在 applicationConfig.xml 文件中定义 xml 名称空间和模式位置定义。还添加任务: 注释驱动,以支持基于注释的任务调度。

    applicationConfig.xmlxmlns:task="Http://www.springframework.org/schema/task"http://www.springframework.org/schema/task/http://www.springframework.org/schema/task/spring-task-3.0.xsd <task:annotation-driven>

    上面的添加是必要的,因为我们将使用基于注释的配置。

    1.3 使用@Scheduled 注释

    下一步是在类中创建一个类和一个方法,如下所示:

    DemoService.javapublic class DemoService{  @Scheduled(cron="*/5 * * * * ?")  public void demoServiceMethod()  {    System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date());  }}

    在上面的例子中

    • 使用@Scheduled 注释反过来会使 Spring 容器理解这个注释下面的方法将作为作业运行。

    • 记住,带@Scheduled 注释的方法不应该有传递给它们的参数。

    • 它们也不应该返回任何值

    • 如果希望在@Scheduled 方法中使用外部对象,应该使用自动连接将它们注入到 DemoService 类中,而不是将它们作为参数传递给@Scheduled 方法。

    二、固定的延时和频率使用@Scheduled

    在这个方法中,fixedDelay 属性与@Scheduled 注释一起使用。

    举例:

    DemoServiceBasicUsageFixedDelay.javapackage com.howtodoinjava.service; import java.util.Date;import org.springframework.scheduling.annotation.Scheduled; public class DemoServiceBasicUsageFixedDelay{ &nbsp;@Scheduled(fixedDelay = 5000) &nbsp;//@Scheduled(fixedRate = 5000)  //Or use this &nbsp;public void demoServiceMethod()  { &nbsp; &nbsp;System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date());  }}

    应用程序配置如下:

    applicationContext.xml< ?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:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" 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.xsd    http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd        http://www.springframework.org/schema/task/ http://www.springframework.org/schema/task/spring-task-3.0.xsd">    <task:annotation-driven />    <bean id="demoServiceBasicUsageFixedDelay" class="com.howtodoinjava.service.DemoServiceBasicUsageFixedDelay"></bean></beans>

    三、配合cron表达式使用@Scheduled

    在此方法中,cron 属性与@Scheduled 注释一起使用。

    举例:

    DemoServiceBasicUsageCron.javapackage com.howtodoinjava.service;import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;public class DemoServiceBasicUsageCron{  @Scheduled(cron="*/5 * * * * ?")  public void demoServiceMethod()  {    System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date());  }}

    应用程序配置如下:

    applicationContext.xml< ?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:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" 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.xsd    http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd        http://www.springframework.org/schema/task/ http://www.springframework.org/schema/task/spring-task-3.0.xsd">    <task:annotation-driven />    <bean id="demoServiceBasicUsageCron" class="com.howtodoinjava.service.DemoServiceBasicUsageCron"></bean></beans>

    四、使用properties文件配置Cron

    在这个方法中,cron 属性与@Scheduled 注释一起使用。此属性的值必须是 cron 表达式,如前面的方法所示,但是,此 cron 表达式将在属性文件中定义,相关属性的键将用于@Scheduled 注释。

    这将使 cron 表达式与源代码分离,从而使更改变得容易。

    DemoServicePropertiesExample.javapackage com.howtodoinjava.service;import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;public class DemoServicePropertiesExample {  @Scheduled(cron = "${cron.expression}")  public void demoServiceMethod()  {    System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date());  }}

    应用程序配置如下:

    applicationContext.xml<?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:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" 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.xsd    http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd        http://www.springframework.org/schema/task/ http://www.springframework.org/schema/task/spring-task-3.0.xsd">    <task:annotation-driven />    <util:properties id="applicationProps" location="application.properties" />  <context:property-placeholder properties-ref="applicationProps" />    <bean id="demoServicePropertiesExample" class="com.howtodoinjava.service.DemoServicePropertiesExample"></bean></beans>

    五、使用context配置Cron

    该方法在属性文件中配置 cron 表达式,在配置文件中使用 cron 表达式的属性键配置作业调度。主要的变化是您不需要在任何方法上使用@Scheduled 注释。方法配置也是在应用程序配置文件中完成的。

    举例:

    DemoServiceXmlConfig.javapackage com.howtodoinjava.service;import java.util.Date;public class DemoServiceXmlConfig{  public void demoServiceMethod()  {    System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date());  }}

    应用程序配置如下:

    applicationContext.xml<?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:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" 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.xsd    http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd        http://www.springframework.org/schema/task/ http://www.springframework.org/schema/task/spring-task-3.0.xsd">    <task:annotation-driven />    <util:properties id="applicationProps" location="application.properties" />  <context:property-placeholder properties-ref="applicationProps" />  <bean id="demoServiceXmlConfig" class="com.howtodoinjava.service.DemoServiceXmlConfig" />  <task:scheduled-tasks>      <task:scheduled ref="demoServiceXmlConfig" method="demoServiceMethod" cron="#{applicationProps['cron.expression']}"></task:scheduled>  </task:scheduled-tasks></beans>

    到此,关于“Spring中的@Scheduled功能怎么使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

    --结束END--

    本文标题: Spring中的@Scheduled功能怎么使用

    本文链接: https://www.lsjlt.com/news/326609.html(转载时请注明来源链接)

    有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

    本篇文章演示代码以及资料文档资料下载

    下载Word文档到电脑,方便收藏和打印~

    下载Word文档
    猜你喜欢
    • Spring中的@Scheduled功能怎么使用
      这篇文章主要介绍“Spring中的@Scheduled功能怎么使用”,在日常操作中,相信很多人在Spring中的@Scheduled功能怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Spring中的@...
      99+
      2023-06-29
    • Spring中@Scheduled功能的使用方法详解
      目录前言一、Spring @Scheduled Annotation1.2 如何启用@Scheduled 注释1.3 使用@Scheduled 注释二、固定的延时和频率使用@Sche...
      99+
      2024-04-02
    • 怎么在不使用spring框架中使用aop的功能
      本篇文章为大家展示了怎么在不使用spring框架中使用aop的功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。Spring框架的AOP机制可以让开发者把业务流程中的通用功能抽取出来,单独编写功能代...
      99+
      2023-06-22
    • 我劝你谨慎使用Spring中的@Scheduled注解
      目录引言1.@Scheduled失效原因2.解析流程图3.使用新的方法schedule定时任务修改表达式无效引言 在一些业务场景中需要执行定时操作来完成一些周期性的任务,比如每隔一周...
      99+
      2024-04-02
    • spring @Scheduled注解的使用误区及解决
      目录@Scheduled注解的使用误区@Scheduled注解各参数详解1、cron2. zone3. fixedDelay4. fixedDelayString5. fixedRa...
      99+
      2024-04-02
    • spring scheduled单线程和多线程使用的坑怎么解决
      本篇内容介绍了“spring scheduled单线程和多线程使用的坑怎么解决”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!公司在...
      99+
      2023-06-29
    • Spring Security单点登录的权限功能怎么使用
      这篇文章主要介绍“Spring Security单点登录的权限功能怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Spring Security单点登录的权限功能怎么使用”...
      99+
      2023-06-29
    • Vue3中的Teleport功能怎么使用
      这篇文章主要介绍“Vue3中的Teleport功能怎么使用”,在日常操作中,相信很多人在Vue3中的Teleport功能怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Vue3中的Teleport功能怎...
      99+
      2023-07-02
    • 如何在不使用spring框架中使用aop的功能
      目录spring提供了两种方式的AOP使用使用xml配置方式使用注解方式AspectJ简介AspectJ的使用实例不使用spring的aop功能实现日志输出第一种第二种:通过面向接口...
      99+
      2024-04-02
    • Fastai中的callbacks功能怎么使用
      在Fastai中,callbacks是用来在训练过程中添加额外功能的工具。通过callbacks,用户可以在训练过程中实现各种操作,...
      99+
      2024-04-02
    • webpack5的功能怎么使用
      这篇文章主要介绍了webpack5的功能怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇webpack5的功能怎么使用文章都会有所收获,下面我们一起来看看吧。功能清除清理已弃用的功能所有在 webpack...
      99+
      2023-06-27
    • 怎么使用Spring注解实现循环重试功能
      这篇文章主要介绍“怎么使用Spring注解实现循环重试功能”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么使用Spring注解实现循环重试功能”文章能帮助大家解决问题。一、@Retryable是什...
      99+
      2023-07-05
    • Spring Cloud中怎么使用Redis实现点赞和取消点赞功能
      Spring Cloud中怎么使用Redis实现点赞和取消点赞功能,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一、Redis 缓存设计及实现...
      99+
      2024-04-02
    • 使用Spring注解怎么实现Bean自动装配功能
      这篇文章将为大家详细讲解有关使用Spring注解怎么实现Bean自动装配功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。使用须知:导入约束:context约束配置注解的支持: contex...
      99+
      2023-06-14
    • 怎么使用Python功能
      这篇文章主要讲解了“怎么使用Python功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么使用Python功能”吧!变量赋值正如函数*args和**kwargs,在变量赋值中可以运用相同...
      99+
      2023-06-16
    • 怎么使用ADO.NET功能
      这篇文章主要为大家展示了“怎么使用ADO.NET功能”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“怎么使用ADO.NET功能”这篇文章吧。ADO.NET功能作为一个高效的.NET程序语言。其混合...
      99+
      2023-06-17
    • 怎么使用Component的padding功能
      这篇文章主要讲解了“怎么使用Component的padding功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么使用Component的padding功能”吧!导入组件@ui5/webc...
      99+
      2023-06-04
    • Python folium的功能怎么使用
      本文小编为大家详细介绍“Python folium的功能怎么使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Python folium的功能怎么使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知...
      99+
      2023-07-04
    • Spring中的Bean怎么使用
      本文小编为大家详细介绍“Spring中的Bean怎么使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Spring中的Bean怎么使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。从广义上 Spring 注解可...
      99+
      2023-07-05
    • PyTorch的TensorDataset功能怎么使用
      本文小编为大家详细介绍“PyTorch的TensorDataset功能怎么使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“PyTorch的TensorDataset功能怎么使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来...
      99+
      2023-07-05
    软考高级职称资格查询
    编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
    • 官方手机版

    • 微信公众号

    • 商务合作