我有一个非常简单的web应用程序来测试@调度注释。类read()中的方法RetrievePrices被注释为@Scheduled。在部署Tomcat上的war之后,我期望每5秒执行一次read方法,但是Tomcat控制台中没有显示任何内容。
主SpringBoot类
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);
}
}类有一个带有@调度的注释的方法
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配置类
package com.aaaa.config;
@Configuration
@ComponentScan("com.aaaa")
@EnableScheduling
public class MyConfiguration {
}POM
<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
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 ************** ");
}
}发布于 2018-02-10 12:20:27
您的RetrievePrices类没有任何类型的注释可供Spring扫描使用。例如,添加@Component注释,它应该运行得很好。
示例:
@SpringBootApplication(scanBasePackages = { "com.aaaa" })
@EnableScheduling
public class BootApplication {
//
} 发布于 2018-08-08 05:34:38
在SpringBoot (工作代码)中创建一个简单的调度程序
这个简单的测试代码正在为我工作。
发布于 2020-11-20 13:14:01
如果使用springBoot 2.4.x (Spring5版本),要使@调度工作,就必须在一个独立的文件类中定义它的方法,而不是@SpringBootApplication文件注释的类。新的类必须是一个组件,所以必须用@Component注释,还必须在相同的类级别添加注释@EnableScheduling,以便所有的东西都能工作。希望这能帮上忙。
https://stackoverflow.com/questions/48720667
复制相似问题