首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpringBoot @Scheduled不工作

SpringBoot @Scheduled不工作
EN

Stack Overflow用户
提问于 2018-02-10 12:16:09
回答 4查看 18.5K关注 0票数 9

我有一个非常简单的web应用程序来测试@调度注释。类read()中的方法RetrievePrices被注释为@Scheduled。在部署Tomcat上的war之后,我期望每5秒执行一次read方法,但是Tomcat控制台中没有显示任何内容。

主SpringBoot类

代码语言:javascript
复制
package com.aaaa.main;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
public class BatchMain {

    public static void main(String[] args) {

        SpringApplication.run(BatchMain.class, args);
    }

}

类有一个带有@调度的注释的方法

代码语言:javascript
复制
package com.aaaa.schedule;

import org.springframework.scheduling.annotation.Scheduled;

public class RetrievePrices {

    @Scheduled(fixedRate = 5000)
    public void read() {

        System.out.println(" *************  Scheduled(fixedRate = 5000) ");
}

一个非常简单的Spring配置类

代码语言:javascript
复制
package com.aaaa.config;

@Configuration
@ComponentScan("com.aaaa")
@EnableScheduling
public class MyConfiguration {
}

POM

代码语言:javascript
复制
<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>
    <artifactId>batch</artifactId>
    <name>Batch processes</name>
    <packaging>war</packaging>

    <properties>
        <java.version>1.8</java.version>
        <Postgres.version>9.4-1200-jdbc41</Postgres.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>

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

更新的类RetrievePrices

代码语言:javascript
复制
package com.aaaa.schedule;

import javax.annotation.PostConstruct;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class RetrievePrices {

    @Scheduled(fixedRate = 5000)
    public void read() {

        System.out.println(" *************  into @Scheduled(fixedRate = 5000) ");
    }

    @PostConstruct
    public void postConstruct() {
        System.out.println(" *************  postConstruct ************** ");
    }
}
EN

回答 4

Stack Overflow用户

发布于 2018-02-10 12:20:27

您的RetrievePrices类没有任何类型的注释可供Spring扫描使用。例如,添加@Component注释,它应该运行得很好。

示例:

代码语言:javascript
复制
@SpringBootApplication(scanBasePackages = { "com.aaaa" })
@EnableScheduling
public class BootApplication {

//
} 
票数 8
EN

Stack Overflow用户

发布于 2018-08-08 05:34:38

在SpringBoot (工作代码)中创建一个简单的调度程序

  1. 应用类 包org.springframework.boot.autoconfigure.SpringBootApplication;com.springboot.test;导入org.springframework.boot.SpringApplication;导入com.springboot.test @EnableScheduling @SpringBootApplication公共类TestApplication {公共静态void (String[] args) { SpringApplication.run(TestApplication.class,args);}
  2. 控制器类 包org.springframework.scheduling.annotation.Scheduled;com.springboot.test;导入org.springframework.web.bind.annotation.RestController;@RestController公共类SchedulerTest { @Scheduled(fixedRate=2000)公共虚名(){ System.out.println("HI");}
  3. pom.xml

这个简单的测试代码正在为我工作。

票数 5
EN

Stack Overflow用户

发布于 2020-11-20 13:14:01

如果使用springBoot 2.4.x (Spring5版本),要使@调度工作,就必须在一个独立的文件类中定义它的方法,而不是@SpringBootApplication文件注释的类。新的类必须是一个组件,所以必须用@Component注释,还必须在相同的类级别添加注释@EnableScheduling,以便所有的东西都能工作。希望这能帮上忙。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48720667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档