@Scheduled定时不执行
1. 未开启定时
解决方案: 在启动类增加注解 @EnableScheduling
@EnableScheduling
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
2. 任务没有被Spring扫描到
解决方案: 在定时任务所在类增加 @Component 或 Service注解
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Service
public class MyService {
    @Scheduled(cron = "0/30 * * * * ?")
    public void performTask() {
        System.out.println("定时任务正在执行...");
        // 其他定时任务的逻辑
    }
}
3. 定时任务写在 Controller类中
解决方案: 将定时任务写在Service层,或将定时任务单独抽离出来,放到一个独立的Service类,并用@Component注解
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class MyScheduledService {
    @Scheduled(fixedRate = 1000)
    public void performTask() {
        System.out.println("定时任务正在执行...");
        // 其他定时任务的逻辑
    }
}