项目中没有@EnableScheduling注解的scheduler如何运行

How the scheduler is running without @EnableScheduling annotation in the project

我有一个 Spring 引导应用程序,它在其他 Jar 上 referring/depends。 Jar 基本上是一个 Spring 应用程序,它作为其中的调度程序方法。

我在 Spring 引导应用程序和外部 Jar 中也没有任何 @EnableScheduling 注释。

但是一旦我 运行 Spring 启动,调度程序就会启动。

如果没有 @EnableScheduling 注释,我不明白是什么导致调度程序开始执行。

我仔细阅读了 M. Deinum 的以下回答,他说@EnableScheduling 将作为 springboot 执行器(版本 1.3.0)的一部分添加。

是的,我的类路径中有一个 Spring 引导执行器,我使用的 Spring 引导版本是 2.0.3。

我如何才能发现是哪个组件导致了调度启动?以及避免调度的可能方法。

因为你使用的是Spring引导,它会根据你的class路径和配置classes自动配置项目。然后你恰好在你的 class 路径中有 spring-boot-starter-actuator 依赖,spring-boot-starter-actuator 中的一个功能是计划任务。 (Actuator @Configuration class 已经添加了@EnableScheduling,所以Spring Boot 会检测到它并配置到你的项目中,之后你的项目也会有这个功能)。

但是作为 Spring Boot 2,执行器默认情况下不启用调度,我无法在我的环境中复制这个问题,我使用的是 2.2.5.RELEASE,所以也许更新你的 spring 引导版本将有助于避免这种情况。


我无法在我的 spring boot 2.0.3 环境中重现您的情况。下面是我的代码片段。

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ouyang</groupId>
    <artifactId>Testing</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Store</name>
    <description>Testing</description>

    <properties>
        <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-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

主要Class

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }

    @Scheduled(fixedDelay = 1000)
    public void test() {

        System.out.println("HI");

    }

}

@Scheduled 方法 test() 未被调用。