🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Spring `@Scheduled` – 安排任务的 4 种方法 > 原文: [https://howtodoinjava.com/spring-core/spring-scheduled-annotation/](https://howtodoinjava.com/spring-core/spring-scheduled-annotation/) Spring 使用`@Scheduled`注解为基于[ cron 表达式](https://en.wikipedia.org/wiki/Cron)的任务调度和异步方法执行提供了出色的支持。 可以将`@Scheduled`注解与触发器元数据一起添加到方法中。 在本文中,我将展示以 4 种不同方式使用`@Scheduled`功能的方法。 > 阅读更多: [Spring timer 任务](https://howtodoinjava.com/spring/spring-core/2-ways-to-execute-timer-tasks-in-spring-3/) ## 1\. Spring `@Scheduled`注解 `@Scheduled`注解用于任务调度。 触发信息需要与此注解一起提供。 #### 1.1. `fixedDelay` vs `FixedRate` vs `cron` 您可以使用属性`fixedDelay` / `fixedRate` / `cron`提供触发信息。 在哪里: 1. `fixedRate`使 Spring 定期运行任务,即使最后一次调用可能仍在运行。 2. `fixedDelay`特别控制最后一次执行完成时的下一个执行时间。 3. `cron`是源自 Unix cron 实用程序的功能,并根据您的要求提供各种选项。 用法示例如下: `@Scheduled Usages` ```java @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 命名空间和模式位置定义。 还添加`task:annotation-driven`以启用基于注解的任务调度。 `applicationConfig.xml` ```java xmlns: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.java` ```java public class DemoService { @Scheduled(cron="*/5 * * * * ?") public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); } } ``` 在上述示例中: 1. 反过来,使用`@Scheduled`注解将使 Spring 容器了解该注解下面的方法将作为作业运行。 2. 请记住,用`@Scheduled`注解的方法不应将参数传递给它们。 3. 他们也不应返回任何值。 4. 如果希望在`@Scheduled`方法中使用外部对象,则应使用自动装配将它们注入`DemoService`类,而不是将它们作为参数传递给`@Scheduled`方法。 ## 2\. `fixedDelay`和`fixedRate` 在此方法中,`fixedDelay`属性与`@Scheduled`注解一起使用。 或者,也可以使用`fixedRate`。 示例类如下所示: `DemoServiceBasicUsageFixedDelay.java` ```java package com.howtodoinjava.service; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; public class DemoServiceBasicUsageFixedDelay { @Scheduled(fixedDelay = 5000) //@Scheduled(fixedRate = 5000) //Or use this public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); } } ``` 应用程序配置如下所示: `applicationContext.xml` ```java < ?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> ``` ## 3\. 使用 Cron 表达式的`@scheduled` 在此方法中,`cron`属性与`@Scheduled`注解一起使用。 此属性的值必须是 cron 表达式。 示例类如下所示: `DemoServiceBasicUsageCron.java` ```java package 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` ```java < ?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> ``` ## 4\. 属性文件中的 Cron 表达式 在此方法中,`cron`属性与`@Scheduled`注解一起使用。 此属性的值必须为 cron 表达式,如先前方法 BUT 一样,该 cron 表达式将在属性文件中定义,并且相关属性的键将用于`@Scheduled`注解。 **将使 cron 表达式与源代码**脱钩,从而使更改变得容易。 `DemoServicePropertiesExample.java` ```java package 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` ```java <?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> ``` ## 5\. 上下文配置中的 Cron 表达式 在这种方法中,在属性文件中配置了 cron 表达式,并使用属性密钥为 cron 表达式在配置文件中配置了作业调度。 主要变化是,您无需在任何方法上使用`@Scheduled`注解。 方法配置也在应用程序配置文件中完成。 示例类如下所示: `DemoServiceXmlConfig.java` ```java package 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` ```java <?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> ``` [下载源码](https://docs.google.com/file/d/0B7yo2HclmjI4Qk5OdkhTeWhxMWc/edit?usp=sharing) 让我知道我是否遗漏任何东西。 学习愉快! 参考: [http://forum.springsource.org/showthread.php?83053-Feature-Scheduled-with-Value-cron-expression](http://forum.springsource.org/showthread.php?83053-Feature-Scheduled-with-Value-cron-expression)