Spring-boot调度程序在没有@EnableScheduling批注的情况下运行

Raj*_*ami 6 spring spring-boot

我按照这个例子在示例项目中创建了一个计划任务:https: //spring.io/guides/gs/scheduling-tasks

它说, @EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.

但是,我错误地没有使用它.怎么还能运作?

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

其他课程:

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
   SpringApplication.run(DemoApplication.class, args);
  }
 }

@Component
public class ScheduledTaskRunner {

    Logger log = LoggerFactory.getLogger(ScheduledTaskRunner.class);

    @Scheduled(cron = "0/1 * * * * *")
    public void run(){
        log.info("Hello");
    }
}
Run Code Online (Sandbox Code Playgroud)

是不是计划的任务不应该运行?

M. *_*num 12

您的代码添加了spring-boot-starter-actuator添加的时间,也添加了调度程序.执行器使用该调度程序来安排度量的自动导出.

这个代码是MetricExportAutoConfiguration在Spring Boot 1.3.0中添加的.

因此,如果你删除执行器调度将不再工作.

  • spring-session 将[添加](https://github.com/spring-projects/spring-session/blob/090a10fb1032d27d1be9fd7a34d99a57efc261ef/spring-session-data-redis/src/main/java/org/springframework/session/data/ redis/config/annotation/web/http/RedisHttpSessionConfiguration.java#L350) `@EnableScheduling` 也是如此。 (2认同)